Functions for manipulating byte arrays including concatenation, padding, and zero-stripping operations commonly needed when working with binary data.
Combines multiple BytesLike values into a single Uint8Array.
/**
* Concatenate multiple BytesLike items into single Uint8Array
* @param items - Array of BytesLike values to concatenate
* @returns Uint8Array containing all items concatenated in order
*/
function concat(items: ReadonlyArray<BytesLike>): Uint8Array;Usage Examples:
import { concat, arrayify } from "@ethersproject/bytes";
// Concatenate hex strings
const result1 = concat(["0x1234", "0xabcd", "0xef"]);
// Result: Uint8Array([0x12, 0x34, 0xab, 0xcd, 0xef])
// Concatenate mixed types
const bytes = new Uint8Array([1, 2]);
const result2 = concat([bytes, "0x0304", [5, 6]]);
// Result: Uint8Array([1, 2, 3, 4, 5, 6])
// Concatenate empty arrays
const result3 = concat(["0x", [], "0x1234"]);
// Result: Uint8Array([0x12, 0x34])
// Build larger byte arrays
const header = "0x1234";
const body = arrayify([171, 205, 239]);
const footer = "0xff";
const packet = concat([header, body, footer]);
// Result: Uint8Array([0x12, 0x34, 0xab, 0xcd, 0xef, 0xff])Removes leading zero bytes from BytesLike data.
/**
* Remove leading zero bytes from BytesLike value
* @param value - BytesLike value to strip zeros from
* @returns Uint8Array with leading zeros removed
*/
function stripZeros(value: BytesLike): Uint8Array;Usage Examples:
import { stripZeros } from "@ethersproject/bytes";
// Strip leading zeros from hex string
const result1 = stripZeros("0x00001234");
// Result: Uint8Array([0x12, 0x34])
// Strip from byte array
const result2 = stripZeros([0, 0, 0, 18, 52]);
// Result: Uint8Array([18, 52])
// All zeros case
const result3 = stripZeros("0x0000");
// Result: Uint8Array([]) - empty array
// No leading zeros - no change
const result4 = stripZeros("0x1234");
// Result: Uint8Array([0x12, 0x34])
// Mixed with actual data
const result5 = stripZeros(new Uint8Array([0, 0, 255, 0, 128]));
// Result: Uint8Array([255, 0, 128]) - only leading zeros removedPads BytesLike data with leading zeros to reach a specified byte length.
/**
* Pad BytesLike value with leading zeros to specified length
* @param value - BytesLike value to pad
* @param length - Target byte length
* @returns Uint8Array padded to specified length
* @throws Error if value is longer than target length
*/
function zeroPad(value: BytesLike, length: number): Uint8Array;Usage Examples:
import { zeroPad } from "@ethersproject/bytes";
// Pad hex string to 4 bytes
const result1 = zeroPad("0x1234", 4);
// Result: Uint8Array([0, 0, 0x12, 0x34])
// Pad byte array to 8 bytes
const result2 = zeroPad([255, 128], 8);
// Result: Uint8Array([0, 0, 0, 0, 0, 0, 255, 128])
// Pad single byte to word (32 bytes)
const result3 = zeroPad("0xff", 32);
// Result: Uint8Array([0, 0, ..., 0, 255]) - 31 zeros + 255
// No padding needed (exact length)
const result4 = zeroPad("0x1234", 2);
// Result: Uint8Array([0x12, 0x34])
// Error case - value too long
try {
zeroPad("0x123456", 2); // 3 bytes > 2 bytes target
} catch (error) {
console.log("Error: value out of range");
}