如果二进制数据实际上是一个字符串怎么办?例如,我们收到一个包含文本数据的文件。
内置的 TextDecoder 对象允许您在给定缓冲区和编码的情况下将值读入实际的 JavaScript 字符串。
我们首先需要创建它
let decoder = new TextDecoder([label], [options]);
label– 编码,默认情况下为utf-8,但也支持big5、windows-1251和许多其他编码。options– 可选对象fatal– 布尔值,如果为true,则对无效(不可解码)字符抛出异常,否则(默认)将它们替换为字符\uFFFD。ignoreBOM– 布尔值,如果为true,则忽略 BOM(可选的字节顺序 Unicode 标记),很少需要。
…然后解码
let str = decoder.decode([input], [options]);
input– 要解码的BufferSource。options– 可选对象stream– 当decoder被重复调用以处理传入的数据块时,用于解码流,值为true。在这种情况下,多字节字符有时可能会在块之间分割。此选项告诉TextDecoder记住“未完成”的字符,并在下一个块到达时对其进行解码。
例如
let uint8Array = new Uint8Array([72, 101, 108, 108, 111]);
alert( new TextDecoder().decode(uint8Array) ); // Hello
let uint8Array = new Uint8Array([228, 189, 160, 229, 165, 189]);
alert( new TextDecoder().decode(uint8Array) ); // 你好
我们可以通过为其创建子数组视图来解码缓冲区的一部分
let uint8Array = new Uint8Array([0, 72, 101, 108, 108, 111, 0]);
// the string is in the middle
// create a new view over it, without copying anything
let binaryString = uint8Array.subarray(1, -1);
alert( new TextDecoder().decode(binaryString) ); // Hello
TextEncoder
TextEncoder 做相反的事情 - 将字符串转换为字节。
语法是
let encoder = new TextEncoder();
它只支持“utf-8”编码。
它有两个方法
encode(str)– 从字符串返回Uint8Array。encodeInto(str, destination)– 将str编码到destination中,它必须是Uint8Array。
let encoder = new TextEncoder();
let uint8Array = encoder.encode("Hello");
alert(uint8Array); // 72,101,108,108,111
评论
<code>标签,对于多行代码,请将它们包装在<pre>标签中,对于超过 10 行的代码,请使用沙箱(plnkr,jsbin,codepen…)