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]