Table of Contents
1. Overview
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 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, whendecoderis called repeatedly with incoming chunks of data. In that case a multi-byte character may occasionally split between chunks. This options tellsTextDecoderto 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
2. 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)– returnsUint8Arrayfrom a string.encodeInto(str, destination)– encodesstrintodestinationthat must beUint8Array.
let encoder = new TextEncoder();
let uint8Array = encoder.encode("Hello");
alert(uint8Array); // 72,101,108,108,111
Related posts:
JavaScript Global object
JavaScript Moving the mouse: mouseover/out, mouseenter/leave
JavaScript Fetch
JavaScript Recursion and stack
JavaScript Element size and scrolling
JavaScript Focusing: focus/blur
JavaScript Anchors: string start ^ and end $
JavaScript Shadow DOM
JavaScript Keyboard: keydown and keyup
JavaScript WebSocket
JavaScript Cookies, document.cookie
JavaScript Ninja code
JavaScript Map and Set
JavaScript Interaction: alert, prompt, confirm
JavaScript Destructuring assignment
JavaScript Arrays
JavaScript Data types
JavaScript Debugging in the browser
JavaScript Dynamic imports
JavaScript Basic operators, maths
JavaScript Comments
JavaScript From the orbital height
JavaScript Coordinates
JavaScript Event loop: microtasks and macrotasks
JavaScript Logical operators
JavaScript Nullish coalescing operator '??'
JavaScript Automated testing with Mocha
JavaScript Multiline mode of anchors ^ $, flag "m"
JavaScript Quantifiers +, *, ? and {n}
JavaScript Methods of RegExp and String
JavaScript Error handling, "try...catch"
JavaScript Methods of primitives