ArrayBuffer转String: 解决中文乱码(模板)
function ab2str(buf) {
return new Uint16Array(buf)
// encodedString = String.fromCodePoint.apply(null, new Uint16Array(buf));
// // decodedString = encodeURI(encodedString);//没有这一步中文会乱码
// // console.log(decodedString);
// return encodedString
}
string转ArrayBuffer(模板)
function str2ab(str) {
var buf = new ArrayBuffer(str.length * 2); // 每个字符占用2个字节
var bufView = new Uint16Array(buf);
for (var i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
字符串转Uint8Array(模板)
function stringToUint8Array(str){
var arr = [];
for (var i = 0, j = str.length; i < j; ++i) {
arr.push(str.charCodeAt(i));
}
var tmpUint8Array = new Uint8Array(arr);
return tmpUint8Array
}
Uint8Array转字符串(模板)
function Uint8ArrayToString(fileData){
console.log(fileData)
var dataString = "";
for (var i = 0; i < fileData.length; i++) {
dataString += String.fromCharCode(fileData[i]);
}
return dataString
}