Kit
Functions

getBaseXCodec

getBaseXCodec(alphabet): VariableSizeCodec<string>

Returns a codec for encoding and decoding base-X strings.

This codec serializes strings using a custom alphabet, treating the length of the alphabet as the base. The encoding process converts the input string into a numeric value in base-X, which is then encoded as bytes. The decoding process reverses this transformation to reconstruct the original string.

This codec supports leading zeroes by treating the first character of the alphabet as the zero character.

Parameters

ParameterTypeDescription
alphabetstringThe set of characters defining the base-X encoding.

Returns

VariableSizeCodec<string>

A VariableSizeCodec<string> for encoding and decoding base-X strings.

Example

Encoding and decoding a base-X string using a custom alphabet.

const codec = getBaseXCodec('0123456789abcdef');
const bytes = codec.encode('deadface'); // 0xdeadface
const value = codec.decode(bytes);      // "deadface"

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 base-X codec, consider using fixCodecSize.

const codec = fixCodecSize(getBaseXCodec('0123456789abcdef'), 8);

If you need a size-prefixed base-X codec, consider using addCodecSizePrefix.

const codec = addCodecSizePrefix(getBaseXCodec('0123456789abcdef'), getU32Codec());

Separate getBaseXEncoder and getBaseXDecoder functions are available.

const bytes = getBaseXEncoder('0123456789abcdef').encode('deadface');
const value = getBaseXDecoder('0123456789abcdef').decode(bytes);

See

On this page