最近遇到了一个比较奇葩的问题,字符串-->base64,但是里边还涉及到了gbk编码问题,代码如下: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; i++) { 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; i++) { binary += String.fromCharCode( bytes[ i ] ); } return window.btoa( binary );}怎么把字符串再转回base64(gbk编码格式)???
1 回答
一只甜甜圈
TA贡献1836条经验 获得超5个赞
反过来就好啦
var arrayBuffer = new TextEncoder("gbk").encode(content);
var content_base64 =_arrayBufferToBase64(arrayBuffer)
添加回答
举报
0/150
提交
取消