TextDecoder JavaScript
TextDecoder :
let decoder = new TextDecoder([label], [options]);
labelutf-8big5,windows-1251options:fatalboolean,true()\uFFFDignoreBOMboolean,trueBOM ()
:
let str = decoder.decode([input], [options]);
inputBufferSourceoptions:streamdecodertrueTextDecoder
:
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) ); //
(subarray):
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
2:
encode(str)Uint8ArrayencodeInto(str, destination)strdestinationdestinationUint8Array
let encoder = new TextEncoder();
let uint8Array = encoder.encode("Hello");
alert(uint8Array); // 72,101,108,108,111
<code><pre>10(plnkr, JSBin, codepen)