相关文章推荐
忧郁的海龟  ·  JS 实现 ...·  1 月前    · 
温柔的烤红薯  ·  vite 的实现浅析 - 知乎·  1 年前    · 
飘逸的登山鞋  ·  创建简单Intellij java ...·  1 年前    · 
低调的海豚  ·  qt http 下载文件-掘金·  1 年前    · 

arraybuffer转string utf8

在 JavaScript 中,您可以使用 TextDecoder 对象将 ArrayBuffer 转换为字符串。如下代码:

const arrayBuffer = new Uint8Array([72, 101, 108, 108, 111]);
const decoder = new TextDecoder("utf-8");
const decodedString = decoder.decode(arrayBuffer);
console.log(decodedString); // Hello

您也可以使用 TextEncoder 对象将字符串转换为 ArrayBuffer:

const string = 'Hello';
const encoder = new TextEncoder();
const encoded = encoder.encode(string);
console.log(encoded); // Uint8Array [ 72, 101, 108, 108, 111 ]
  •