padBytes

Call Signature

function padBytes(bytes, length): Uint8Array;

Pads a Uint8Array with zeroes to the specified length. If the array is longer than the specified length, it is returned as-is.

Parameters

ParameterTypeDescription
bytesUint8ArrayThe byte array to pad.
lengthnumberThe desired length of the byte array.

Returns

Uint8Array

Examples

Adds zeroes to the end of the byte array to reach the desired length.

const bytes = new Uint8Array([0x01, 0x02]);
const paddedBytes = padBytes(bytes, 4);
//    ^ [0x01, 0x02, 0x00, 0x00]

Returns the original byte array if it is already at the desired length.

const bytes = new Uint8Array([0x01, 0x02]);
const paddedBytes = padBytes(bytes, 2);
// bytes === paddedBytes

Call Signature

function padBytes(bytes, length): ReadonlyUint8Array;

Pads a Uint8Array with zeroes to the specified length. If the array is longer than the specified length, it is returned as-is.

Parameters

ParameterTypeDescription
bytesReadonlyUint8ArrayThe byte array to pad.
lengthnumberThe desired length of the byte array.

Returns

ReadonlyUint8Array

Examples

Adds zeroes to the end of the byte array to reach the desired length.

const bytes = new Uint8Array([0x01, 0x02]);
const paddedBytes = padBytes(bytes, 4);
//    ^ [0x01, 0x02, 0x00, 0x00]

Returns the original byte array if it is already at the desired length.

const bytes = new Uint8Array([0x01, 0x02]);
const paddedBytes = padBytes(bytes, 2);
// bytes === paddedBytes

On this page