Skip to main content
Version: 6.11.0

Namespace: encode

References​

IS_BROWSER​

Re-exports IS_BROWSER

Functions​

arrayBufferToString​

â–¸ arrayBufferToString(array): string

Convert array buffer to string

[internal usage]

Parameters​

NameTypeDescription
arrayArrayBufferThe ArrayBuffer to convert to string.

Returns​

string

The converted string.

Example

const buffer = new ArrayBuffer(5);
const view = new Uint8Array(buffer);
[72, 101, 108, 108, 111].forEach((x, idx) => (view[idx] = x));
const result = encode.arrayBufferToString(buffer);
// result = "Hello"

Defined in​

src/utils/encode.ts:30


utf8ToArray​

â–¸ utf8ToArray(str): Uint8Array

Convert utf8-string to Uint8Array

[internal usage]

Parameters​

NameTypeDescription
strstringThe UTF-8 string to convert.

Returns​

Uint8Array

The encoded Uint8Array.

Example

const myString = 'Hi';
const result = encode.utf8ToArray(myString);
// result = Uint8Array(2) [ 72, 105 ]

Defined in​

src/utils/encode.ts:49


stringToArrayBuffer​

â–¸ stringToArrayBuffer(str): Uint8Array

Convert utf8-string to Uint8Array

Parameters​

NameType
strstring

Returns​

Uint8Array

Deprecated

equivalent to 'utf8ToArray', alias will be removed

Defined in​

src/utils/encode.ts:58


atobUniversal​

â–¸ atobUniversal(a): Uint8Array

Convert string to array buffer (browser and node compatible)

Parameters​

NameTypeDescription
astringThe Base64 encoded string to convert.

Returns​

Uint8Array

The decoded Uint8Array.

Example

const base64String = 'SGVsbG8='; // 'Hello' in Base64
const result = encode.atobUniversal(base64String);
// result = Uint8Array(5) [ 72, 101, 108, 108, 111 ]

Defined in​

src/utils/encode.ts:75


btoaUniversal​

â–¸ btoaUniversal(b): string

Convert array buffer to string (browser and node compatible)

Parameters​

NameTypeDescription
bArrayBufferThe Array buffer.

Returns​

string

The Base64 encoded string.

Example

const buffer = new Uint8Array([72, 101, 108, 108, 111]); // Array with ASCII values for 'Hello'
const result = encode.btoaUniversal(buffer);
// result = "SGVsbG8="

Defined in​

src/utils/encode.ts:92


buf2hex​

â–¸ buf2hex(buffer): string

Convert array buffer to hex-string

Parameters​

NameTypeDescription
bufferUint8ArrayThe encoded Uint8Array.

Returns​

string

The hex-string

Example

const buffer = new Uint8Array([72, 101, 108, 108, 111]); // Array with ASCII values for 'Hello'
const result = encode.buf2hex(buffer);
// result = "48656c6c6f"

Defined in​

src/utils/encode.ts:109


removeHexPrefix​

â–¸ removeHexPrefix(hex): string

Remove hex prefix '0x' from hex-string

Parameters​

NameTypeDescription
hexstringhex-string

Returns​

string

The hex-string

Example

const hexStringWithPrefix = '0x48656c6c6f';
const result = encode.removeHexPrefix(hexStringWithPrefix);
// result: "48656c6c6f"

Defined in​

src/utils/encode.ts:125


addHexPrefix​

â–¸ addHexPrefix(hex): string

Add hex prefix '0x' to base16-string

Parameters​

NameTypeDescription
hexstringbase16-string

Returns​

string

The hex-string

Example

const plainHexString = '48656c6c6f';
const result = encode.addHexPrefix(plainHexString);
// result: "0x48656c6c6f"

Defined in​

src/utils/encode.ts:141


padLeft​

â–¸ padLeft(str, length, padding?): string

Prepend string (default with '0')

Pads a string to a certain length with a specific string. The padding can be applied only to the left of the input string.

Parameters​

NameTypeDefault valueDescription
strstringundefinedThe string to pad.
lengthnumberundefinedThe target length for the padded string.
padding?stringSTRING_ZEROThe string to use for padding. Defaults to '0'.

Returns​

string

The padded string.

Example

const myString = '1A3F';
const result = encode.padLeft(myString, 10);
// result: '0000001A3F'

Defined in​

src/utils/encode.ts:194


calcByteLength​

â–¸ calcByteLength(str, byteSize?): number

Calculate byte length of string

[no internal usage]

Calculates the byte length of a string based on a specified byte size. The function rounds up the byte count to the nearest multiple of the specified byte size.

Parameters​

NameTypeDefault valueDescription
strstringundefinedThe string whose byte length is to be calculated.
byteSize?number8The size of the byte block to round up to. Defaults to 8.

Returns​

number

The calculated byte length, rounded to the nearest multiple of byteSize.

Example

const myString = 'Hello';
const result = encode.calcByteLength(myString, 4);
// result = 8 (rounded up to the nearest multiple of 4)

Defined in​

src/utils/encode.ts:218


sanitizeBytes​

â–¸ sanitizeBytes(str, byteSize?, padding?): string

Prepend '0' to string bytes

[no internal usage]

  • Prepends padding to the left of a string to ensure it matches a specific byte length. The function uses a specified padding character and rounds up the string length to the nearest multiple of byteSize.

Parameters​

NameTypeDefault valueDescription
strstringundefinedThe string to be padded.
byteSize?number8The byte block size to which the string length should be rounded up. Defaults to 8.
padding?stringSTRING_ZEROThe character to use for padding. Defaults to '0'.

Returns​

string

The padded string.

Example

const myString = '123';
const result = encode.sanitizeBytes(myString);
// result: '00000123' (padded to 8 characters)

Defined in​

src/utils/encode.ts:245


sanitizeHex​

â–¸ sanitizeHex(hex): string

Sanitizes a hex-string by removing any existing '0x' prefix, padding the string with '0' to ensure it has even length, and then re-adding the '0x' prefix.

[no internal usage]

Parameters​

NameTypeDescription
hexstringhex-string

Returns​

string

format: hex-string

Example

const unevenHex = '0x23abc';
const result = encode.sanitizeHex(unevenHex);
// result = '0x023abc' (padded to ensure even length)

Defined in​

src/utils/encode.ts:264


pascalToSnake​

â–¸ pascalToSnake(text): string

String transformation util

Pascal case to screaming snake case

Parameters​

NameTypeDescription
textstringThe PascalCase string to convert.

Returns​

string

The converted snake_case string in uppercase.

Example

const pascalString = 'PascalCaseExample';
const result = encode.pascalToSnake(pascalString);
// result: 'PASCAL_CASE_EXAMPLE'

Defined in​

src/utils/encode.ts:288