Kit
Functions

padBytes

padBytes(bytes, length): ReadonlyUint8Array | Uint8Array<ArrayBufferLike>

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
bytesReadonlyUint8Array | Uint8Array<ArrayBufferLike>The byte array to pad.
lengthnumberThe desired length of the byte array.

Returns

ReadonlyUint8Array | Uint8Array<ArrayBufferLike>

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