What if the binary data is actually a string? For instance, we received a file with textual data.
The build-in TextDecoder object allows to read the value into an an actual JavaScript string, given the buffer and the encoding.
We first need to create it:
let decoder = new TextDecoder([label], [options]);
label– the encoding,utf-8by default, butbig5,windows-1251and many other are also supported.options– optional object:fatal– boolean, iftruethen throw an exception for invalid (non-decodable) characters, otherwise (default) replace them with character\uFFFD.ignoreBOM– boolean, iftruethen ignore BOM (an optional byte-order unicode mark), rarely needed.
…And then decode:
let str = decoder.decode([input], [options]);
input–BufferSourceto decode.options– optional object:stream– true for decoding streams, whendecoder