Published
Edited
Jun 9, 2020
Insert cell
md`# TextDecoder 和 TextEncoder`
Insert cell
md`## TextDecoder`
Insert cell
md`内建的 TextDecoder 对象在给定缓冲区(buffer)和编码格式(encoding)的情况下,能够将值读取到实际的 JavaScript 字符串中。`
Insert cell
{
let uint8Array = new Uint8Array([72, 101, 108, 108, 111]);
console.log( new TextDecoder().decode(uint8Array) ); // Hello
}
Insert cell
md`## TextEncoder`
Insert cell
md`TextEncoder 做相反的事情 —— 将字符串转换为字节。`
Insert cell
{
let encoder = new TextEncoder();
let uint8Array = encoder.encode("Hello");
console.log(uint8Array); // 72,101,108,108,111
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more