? , .
:
let decoder = new TextDecoder([label], [options]);
label,utf-8,big5,windows-1251.options:fatalboolean,true, ( ) , ( )\uFFFD.ignoreBOMboolean,true, BOM ( , ), .
decode:
let str = decoder.decode([input], [options]);
input(BufferSource) .options:streamtrue ,decoder. .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]);
//
// , .
// .
let binaryString = uint8Array.subarray(1, -1);
alert( new TextDecoder().decode(binaryString) ); // Hello
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)