Skip to content

Function: decodeUtf8() ​

ts
function decodeUtf8(data): string;

Defined in: decodeUtf8.ts:22

Converts UTF-8 bytes to a string. The function uses TextDecoder if the environment has it. Otherwise, the function decodes the bytes itself.

Each invalid byte sequence gives U+FFFD, like in TextDecoder. A leading byte order mark is the one difference. TextDecoder removes the byte order mark. Without TextDecoder, the result keeps it.

If the data is always UTF-8, use this function instead of decodeText. The bundle of this function is smaller.

Parameters ​

data ​

| DataView<ArrayBufferLike> | Uint8Array<ArrayBufferLike>

The bytes to decode.

Returns ​

string

The decoded string.

Example ​

ts
const bytes = new Uint8Array([0x68, 0x69, 0x20, 0xf0, 0x9f, 0x98, 0x80])

equal(decodeUtf8(bytes), 'hi 😀')

A Streaming Video Technology Alliance project.