tencent cloud

Feedback

Encoding

Last updated: 2024-01-30 16:56:26
    The encoder and decoder are designed based on standard Web APIs TextEncoder and TextDecoder.

    TextEncoder

    The TextEncoder API takes code point streams as inputs and generates UTF-8 byte streams as outputs. For more information, see TextEncoder.

    Constructor API

    // The TextEncoder() constructor does not have any parameters.
    const encoder = new TextEncoder();

    Attributes

    // encoder.encoding
    readonly encoding: string;
    The name of the encoding algorithm that is used by the encoder. The value is fixed to UTF-8.

    Methods

    encode

    encoder.encode(input?: string | undefined): Uint8Array
    The encoder.encode() method takes code point streams as inputs and generates UTF-8 byte streams as outputs.
    Note:
    The maximum length of input is 300 MB. An exception will be thrown if the maximum length is exceeded.
    encoder.encode parameters
    Parameter
    Type
    Required
    Description
    input
    string | undefined
    No
    The text to be encoded.

    encodeInto

    encoder.encodeInto(input: string, destination: Uint8Array): EncodeIntoResult;
    The encoder.encodeInto() method takes code point streams as inputs, generates UTF-8 byte streams as outputs, and writes the outputs to a destination byte array.
    Parameters
    Parameter
    Type
    Required
    Description
    input
    string
    Yes
    The text to be encoded.
    destination
    Yes
    The object in which the encoded text is stored.
    Return value: EncodeIntoResult
    Parameter
    Type
    Description
    read
    number
    The number of UTF-16 units that have been converted to UTF-8.
    written
    number
    The number of bytes that are modified in the destination Uint8Array.

    TextDecoder

    The TextDecoder API takes byte streams as inputs and generates code point streams as outputs. For more information, see TextDecoder.

    Construction API

    const decoder = new TextDecoder(label?: string | undefined, options?: DecoderOptions | undefined): TextEncoder;

    Parameters

    Note:
    The label parameter does not support the following values:
    iso-8859-16
    hz-gb-2312
    csiso2022kr, iso-2022-kr
    Parameter
    Type
    Required
    Description
    label
    string | undefined
    No
    The name of the decoding algorithm that is used by the decoder. Default value: UTF-8. For the valid values of label, see Encoding API Encodings.
    options
    DecoderOptions | undefined
    No
    The configuration items of the decoder.

    DecoderOptions

    The following table describes the configuration items of the decoder.
    Parameter
    Type
    Default value
    Description
    fatal
    boolean
    false
    Specifies whether to throw an exception when decoding fails.
    ignoreBOM
    boolean
    false
    Specifies whether to ignore byte-order marker.

    Attributes

    encoding

    // decoder.encoding
    readonly encoding: string;
    The name of the decoding algorithm that is used by the decoder.

    fatal

    // decoder.fatal
    readonly fatal: boolean;
    Specifies whether to throw an exception when decoding fails.

    ignoreBOM

    // decoder.ignoreBOM
    readonly ignoreBOM: boolean;
    Specifies whether to ignore byte-order marker.

    Methods

    decode

    const result = decoder.decode(buffer?: ArrayBuffer | ArrayBufferView | undefined, options?: DecodeOptions | undefined): string;
    Note:
    The maximum length of buffer is 100 MB. An exception will be thrown if the maximum length is exceeded.
    Parameter
    Type
    Required
    Description
    buffer
    No
    The byte stream to be decoded.
    The maximum length of buffer is 100 MB. An exception will be thrown if the maximum length is exceeded.
    options
    No
    The configuration items for decoding.

    DecodeOptions

    The following table describes the configuration items for decoding.
    Parameter
    Type
    Default value
    Description
    stream
    boolean
    false
    Specifies whether to perform decoding in streaming mode. Default value: false. Valid values:
    true
    Perform decoding in streaming mode. Use this value if data is processed in chunks and additional chunks are expected.
    false
    Do not perform decoding in streaming mode. Use this value if the current chunk is the last one or data is not chunked.

    Sample Code

    function handleEvent(event) {
    // Encoder
    const encoder = new TextEncoder();
    const encodeText = encoder.encode('hello world');
    
    // Decoder
    const decoder = new TextDecoder();
    const decodeText = decoder.decode(encodeText);
    
    // Response
    const response = new Response(JSON.stringify({
    encodeText: encodeText.toString(),
    decodeText,
    }));
    
    return response;
    }
    
    addEventListener('fetch', (event) => {
    event.respondWith(handleEvent(event));
    });

    References

    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support