recently encountered a rather bizarre problem, string--> base64, but it also involves gbk coding. The code is as follows:
var content_base64 = "CgkJCgkJCQk8Y29uZGl0aW9ucyB0eXBlPSJBTkQiPgoJCQkJCTxzeXNDb25kaXRpb25zIHR5cGU9Inx8Ij4KCQkJCQk8c3lzQ29uZGl0aW9uIG5hbWU9ItDCzsS1tSIgZXhwcmVzc2lvbj0iS0dselRtVjNSRzlqS0NrcElBPT0iLz4KCQkJCQk8c3lzQ29uZGl0aW9uIG5hbWU9IrWxx7C0psDtyMsiIGV4cHJlc3Npb249IktHbHpRM1Z5VlhObGNpZ3BLU0E9Ii8+CgkJCQkJPC9zeXNDb25kaXRpb25zPgoJCQkJPC9jb25kaXRpb25zPgoJCQk=";
var arrayBuffer = _base64ToArrayBuffer(content_base64);
var content = new TextDecoder("gbk").decode(arrayBuffer);
console.log(content);
function _base64ToArrayBuffer(base64) {
var binary_string = window.atob(base64);
var len = binary_string.length;
var bytes = new Uint8Array( len );
for (var i = 0; i < len; iPP) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes.buffer;
}
function _arrayBufferToBase64( buffer ) {
var binary = "";
var bytes = new Uint8Array( buffer );
var len = bytes.byteLength;
for (var i = 0; i < len; iPP) {
binary += String.fromCharCode( bytes[ i ] );
}
return window.btoa( binary );
}
how to convert the string back to base64 (gbk encoding format) ?