使用TextEncoder或TextDecoder

if (!("TextEncoder" in window)) 
  alert("Sorry, this browser does not support TextEncoder...");

var enc = new TextEncoder(); // always utf-8
console.log(enc.encode("This is a string converted to a Uint8Array"));

TextDecoder 接口代表特定方法的解码器,即特定的字符编码,如 utf-8、iso-8859-2、koi8、cp1261、gbk、…解码器将字节流作为输入并发出 码点流。

if (!("TextDecoder" in window))
  alert("Sorry, this browser does not support TextDecoder...");

var enc = new TextDecoder("utf-8");
var arr = new Uint8Array([84,104,105,115,32,105,115,32,97,32,85,105,110,116,
                          56,65,114,114,97,121,32,99,111,110,118,101,114,116,
                          101,100,32,116,111,32,97,32,115,116,114,105,110,103]);
console.log(enc.decode(arr));

参考:
https://stackoverflow.com/questions/6965107/converting-between-strings-and-arraybuffers
https://blog.csdn.net/u014436243/article/details/119995339

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐