or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

array-operations.mddata-conversion.mdhex-operations.mdindex.mdsignature-operations.mdtype-checking.md
tile.json

hex-operations.mddocs/

Hex String Operations

Advanced utilities for working with hexadecimal string representations of binary data, including length calculations, slicing, concatenation, value normalization, and padding operations.

Capabilities

Get Hex Data Length

Calculates the byte length of hex data.

/**
 * Get the byte length of hex data
 * @param data - BytesLike data to measure
 * @returns Number of bytes represented, or null if invalid hex
 */
function hexDataLength(data: BytesLike): number | null;

Usage Examples:

import { hexDataLength } from "@ethersproject/bytes";

// Calculate length of hex strings
console.log(hexDataLength("0x1234"));      // 2 bytes
console.log(hexDataLength("0x123456"));    // 3 bytes
console.log(hexDataLength("0x"));          // 0 bytes (empty)

// Works with byte arrays too
console.log(hexDataLength([1, 2, 3, 4]));  // 4 bytes

// Invalid hex returns null
console.log(hexDataLength("0x123"));       // null (odd length)
console.log(hexDataLength("invalid"));     // null (not hex)

Slice Hex Data

Extracts a slice from hex data, returning a hex string.

/**
 * Extract slice from hex data
 * @param data - BytesLike data to slice
 * @param offset - Starting byte offset
 * @param endOffset - Optional ending byte offset
 * @returns Hex string slice
 * @throws Error if data is invalid hex format
 */
function hexDataSlice(data: BytesLike, offset: number, endOffset?: number): string;

Usage Examples:

import { hexDataSlice } from "@ethersproject/bytes";

// Slice from hex string
const data = "0x123456789abc";

console.log(hexDataSlice(data, 0, 2));    // "0x1234" (bytes 0-1)
console.log(hexDataSlice(data, 1, 3));    // "0x3456" (bytes 1-2)
console.log(hexDataSlice(data, 2));       // "0x56789abc" (from byte 2 to end)

// Slice from byte array
const bytes = [0x12, 0x34, 0x56, 0x78];
console.log(hexDataSlice(bytes, 1, 3));   // "0x3456"

// Extract specific ranges
const address = "0x1234567890abcdef1234567890abcdef12345678";
const first4 = hexDataSlice(address, 0, 4);    // "0x12345678"
const last4 = hexDataSlice(address, 16);       // "0x12345678"

Concatenate as Hex

Concatenates BytesLike items and returns the result as a hex string.

/**
 * Concatenate BytesLike items as hex string
 * @param items - Array of BytesLike values to concatenate
 * @returns Hex string containing all items concatenated
 */
function hexConcat(items: ReadonlyArray<BytesLike>): string;

Usage Examples:

import { hexConcat } from "@ethersproject/bytes";

// Concatenate hex strings
const result1 = hexConcat(["0x1234", "0xabcd"]);
// Result: "0x1234abcd"

// Concatenate mixed types
const result2 = hexConcat(["0x12", [52, 171], "0xcd"]);
// Result: "0x1234abcd"

// Build complex hex data
const prefix = "0x1234";
const data = new Uint8Array([171, 205]);
const suffix = "0xef";
const combined = hexConcat([prefix, data, suffix]);
// Result: "0x1234abcdef"

Hex Value (Minimal Representation)

Converts a value to minimal hex representation (strips leading zeros but keeps at least "0x0").

/**
 * Convert value to minimal hex representation
 * @param value - BytesLike, Hexable, number, or bigint to convert
 * @returns Minimal hex string (leading zeros stripped, minimum "0x0")
 */
function hexValue(value: BytesLike | Hexable | number | bigint): string;

Usage Examples:

import { hexValue } from "@ethersproject/bytes";

// Minimal representation of numbers
console.log(hexValue(0));         // "0x0"
console.log(hexValue(255));       // "0xff"
console.log(hexValue(4660));      // "0x1234"

// Strip leading zeros from hex
console.log(hexValue("0x00001234")); // "0x1234"
console.log(hexValue("0x0000"));     // "0x0"

// Works with bytes
console.log(hexValue([0, 0, 18, 52])); // "0x1234"

Strip Hex Zeros

Removes leading zeros from hex string representation.

/**
 * Remove leading zeros from hex string
 * @param value - BytesLike value to strip zeros from
 * @returns Hex string with leading zeros removed
 * @throws Error if value is not valid hex string
 */
function hexStripZeros(value: BytesLike): string;

Usage Examples:

import { hexStripZeros } from "@ethersproject/bytes";

// Strip leading zeros
console.log(hexStripZeros("0x00001234")); // "0x1234"
console.log(hexStripZeros("0x0000"));     // "0x"

// Works with bytes (converts to hex first)
console.log(hexStripZeros([0, 0, 18, 52])); // "0x1234"

// No leading zeros - no change
console.log(hexStripZeros("0x1234"));     // "0x1234"

Zero Pad Hex String

Pads hex string with leading zeros to specified byte length.

/**
 * Pad hex string with leading zeros to specified byte length
 * @param value - BytesLike value to pad
 * @param length - Target byte length (hex string will be 2 + 2*length chars)
 * @returns Hex string padded to specified length
 * @throws Error if value exceeds target length
 */
function hexZeroPad(value: BytesLike, length: number): string;

Usage Examples:

import { hexZeroPad } from "@ethersproject/bytes";

// Pad hex string to specific length
console.log(hexZeroPad("0x1234", 4));    // "0x00001234"
console.log(hexZeroPad("0xff", 32));     // "0x00000000000000000000000000000000000000000000000000000000000000ff"

// Pad bytes to hex string
console.log(hexZeroPad([255], 2));       // "0x00ff"

// Address padding (20 bytes)
console.log(hexZeroPad("0x1234", 20));   // "0x0000000000000000000000000000000000001234"

// Error case - value too long
try {
  hexZeroPad("0x123456", 2); // 3 bytes > 2 bytes target
} catch (error) {
  console.log("Error: value out of range");
}