Encoding

Last update:2024-08-14 19:50:30

TextEncoder

The TextEncoder interface takes a stream of Unicode code points as input and outputs a byte stream. Encoding types passed to the constructor are ignored, and a UTF-8 TextEncoder object is always created.

TextEncoder() returns a newly created TextEncoder object, which uses UTF-8 encoding to generate a byte stream. TextEncoder does not accept any parameters and does not throw any exceptions.

Constructor

let encoder = new TextEncoder();

Properties

  • encoder.encoding (read-only)

    The name of the encoder, which describes the encoding method used by the TextEncoder in string format (always utf-8).

Methods

  • encode(input)

    Encodes the string input and returns a byte array of type Uint8Array.

TextDecoder

The TextDecoder interface represents a decoder used to decode byte streams into strings. It creates a UTF-8 decoder by default.

TextDecoder(label, options) returns a newly created TextDecoder object.

Constructor

let decoder = new TextDecoder(label, options);

Parameters:

  • label (Optional): A string representing the encoding label to be used. The default value is utf-8. Supported encoding labels include:

  • options (Optional): An object configuring the decoding behavior. The following properties are supported:

    • fatal: A boolean value indicating whether to handle decoding errors in fatal mode. If true, a TypeError exception will be thrown upon encountering a decoding error; if false (default), the decoding error will be replaced with the Unicode replacement character (U+FFFD).
    • ignoreBOM: A boolean value indicating whether to ignore the Byte Order Mark (BOM).

Properties

  • decoder.encoding (read-only)

    The name of the decoder, which describes the decoding method used by the TextDecoder in string format.

  • decoder.fatal (read-only)

    Indicates whether the error mode is fatal.

  • decoder.ignoreBOM (read-only)

    Indicates whether the Byte Order Mark (BOM) is ignored.

Methods

  • decode(input, options)

    Decodes the input byte array input using the method specified in the TextDecoder object. The options parameter is an optional object for configuring the decoding behavior.

    Parameters:

    • input: The byte array to be decoded, which can be Uint8Array, ArrayBuffer, or other similar types.

    • options (Optional): An object configuring the decoding behavior. The following property is supported:

      • stream: A boolean value indicating whether to decode in streaming mode. If true, the decoder treats the input as a byte stream and decodes as many bytes as possible until the end of the stream or an error is encountered. If false (default), the decoder decodes the entire input into a string.

    Return Value:

    Returns the decoded string.

Code Example

// Create a TextEncoder object
const encoder = new TextEncoder();

// Encode the string "Hello, world!" using UTF-8
const encodedData = encoder.encode("Hello, world!");

// Create a TextDecoder object using GBK encoding
const decoder = new TextDecoder("gbk");

// Decode the byte array
const decodedString = decoder.decode(encodedData);

// Output the decoded string
console.log(decodedString); 

References

Is the content of this document helpful to you?
Yes
I have suggestion
Submitted successfully! Thank you very much for your feedback, we will continue to strive to do better!