eumnq8 发表于 2021-4-1 19:31:53

javascript ArrayBuffer和String转换

function ab2str(buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
}
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 = str.charCodeAt(i);
}
return buf;
}
页: [1]
查看完整版本: javascript ArrayBuffer和String转换