Kit
Functions

getBaseXResliceCodec

getBaseXResliceCodec(alphabet, bits): VariableSizeCodec<string>

Returns a codec for encoding and decoding base-X strings using bit re-slicing.

This codec serializes strings by dividing the input into custom-sized bit chunks, mapping them to a given alphabet, and encoding the result into bytes. It is particularly suited for encoding schemes where the alphabet's length is a power of 2, such as base-16 or base-64.

Parameters

ParameterTypeDescription
alphabetstringThe set of characters defining the base-X encoding.
bitsnumberThe number of bits per encoded chunk, typically log2(alphabet.length).

Returns

VariableSizeCodec<string>

A VariableSizeCodec<string> for encoding and decoding base-X strings using bit re-slicing.

Example

Encoding and decoding a base-X string using bit re-slicing.

const codec = getBaseXResliceCodec('elho', 2);
const bytes = codec.encode('hellolol'); // 0x4aee
const value = codec.decode(bytes);      // "hellolol"

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(getBaseXResliceCodec('elho', 2), 8);

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

const codec = addCodecSizePrefix(getBaseXResliceCodec('elho', 2), getU32Codec());

Separate getBaseXResliceEncoder and getBaseXResliceDecoder functions are available.

const bytes = getBaseXResliceEncoder('elho', 2).encode('hellolol');
const value = getBaseXResliceDecoder('elho', 2).decode(bytes);

See

On this page