Kit
Functions

getUtf8Codec

getUtf8Codec(): VariableSizeCodec<string>

Returns a codec for encoding and decoding UTF-8 strings.

This codec serializes strings using UTF-8 encoding. The encoded output contains as many bytes as needed to represent the string.

Returns

VariableSizeCodec<string>

A VariableSizeCodec<string> for encoding and decoding UTF-8 strings.

Example

Encoding and decoding a UTF-8 string.

const codec = getUtf8Codec();
const bytes = codec.encode('hello'); // 0x68656c6c6f
const value = codec.decode(bytes);   // "hello"

Remarks

This codec does not enforce a size boundary. It will encode and decode all bytes necessary to represent the string.

If you need a fixed-size UTF-8 codec, consider using fixCodecSize.

const codec = fixCodecSize(getUtf8Codec(), 5);

If you need a size-prefixed UTF-8 codec, consider using addCodecSizePrefix.

const codec = addCodecSizePrefix(getUtf8Codec(), getU32Codec());

Separate getUtf8Encoder and getUtf8Decoder functions are available.

const bytes = getUtf8Encoder().encode('hello');
const value = getUtf8Decoder().decode(bytes);

See

On this page