给定下面我用来在 base64 中编码和解码的两个函数。如果我编码然后解码,我只是这样做,那么我的输出和输入之间不会得到相同的值。
知道我做错了什么吗?
function _base64ToArrayBuffer(string_base64) {
var binary_string = window.atob(string_base64);
var len = binary_string.length;
var bytes = new Uint8Array( len );
for (var i = 0; i < len; i++) {
var ascii = string_base64.charCodeAt(i);
bytes[i] = ascii;
}
return bytes.buffer;
}
function _arrayBufferToBase64( array_buffer ) {
var binary = '';
var bytes = new Uint8Array( array_buffer );
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode( bytes[ i ] )
}
return window.btoa( binary );
}
var input = my_ArrayBuffer;
var raw_data_base64 = _arrayBufferToBase64(input);
var ouput = _base64ToArrayBuffer(raw_data_base64);