赞
踩
https://blog.csdn.net/xyzdwf/article/details/82220987
//ArrayBuffer转字符串
function ab2str(u,f) {
var b = new Blob([u]);
var r = new FileReader();
r.readAsText(b, 'utf-8');
r.onload = function (){if(f)f.call(null,r.result)}
}
//字符串转字符串ArrayBuffer
function str2ab(s,f) {
var b = new Blob([s],{type:'text/plain'});
var r = new FileReader();
r.readAsArrayBuffer(b);
r.onload = function (){if(f)f.call(null,r.result)}
}
记录备用,网上有用Uint16Array视图转换的并不行,因为utf-8编码字符字节编码长度是不一定是两个字节。
//实际调用
ab2str(arrayBuffer,function(str){
//str为字符串
});
str2ab(str,function(ab){
//ab为ArrayBuffer
});
---------------------
作者:xyzdwf
来源:CSDN
原文:https://blog.csdn.net/xyzdwf/article/details/82220987
版权声明:本文为博主原创文章,转载请附上博文链接!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。