pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/front42/en.javascript.info/blob/patch-15/4-binary/02-text-decoder/article.md

0d4a.css" /> en.javascript.info/4-binary/02-text-decoder/article.md at patch-15 · front42/en.javascript.info · GitHub
Skip to content

Latest commit

 

History

History
76 lines (50 loc) · 2.42 KB

File metadata and controls

76 lines (50 loc) · 2.42 KB

TextDecoder and TextEncoder

What if the binary data is actually a string? For instance, we received a file with textual data.

The built-in TextDecoder object allows one to read the value into 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-8 by default, but big5, windows-1251 and many other are also supported.
  • options -- optional object:
    • fatal -- boolean, if true then throw an exception for invalid (non-decodable) characters, otherwise (default) replace them with character \uFFFD.
    • ignoreBOM -- boolean, if true then ignore BOM (an optional byte-order Unicode mark), rarely needed.

...And then decode:

let str = decoder.decode([input], [options]);
  • input -- BufferSource to decode.
  • options -- optional object:
    • stream -- true for decoding streams, when decoder is called repeatedly with incoming chunks of data. In that case a multi-byte character may occasionally split between chunks. This options tells TextDecoder to memorize "unfinished" characters and decode them when the next chunk comes.

For instance:

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) ); // 你好

We can decode a part of the buffer by creating a subarray view for it:

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 does the reverse thing -- converts a string into bytes.

The syntax is:

let encoder = new TextEncoder();

The only encoding it supports is "utf-8".

It has two methods:

  • encode(str) -- returns Uint8Array from a string.
  • encodeInto(str, destination) -- encodes str into destination that must be Uint8Array.
let encoder = new TextEncoder();

let uint8Array = encoder.encode("Hello");
alert(uint8Array); // 72,101,108,108,111
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy