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

data-conversion.mddocs/

Data Conversion

Core conversion functions for transforming between different binary data representations including bytes arrays, hex strings, numbers, and objects with hex conversion capabilities.

Capabilities

Arrayify - Convert to Bytes

Converts various input formats to a Uint8Array, the standard bytes representation.

/**
 * Convert BytesLike, Hexable, or number to Uint8Array
 * @param value - Value to convert (BytesLike, Hexable, or number)
 * @param options - Optional conversion options
 * @returns Uint8Array representation of the input
 */
function arrayify(value: BytesLike | Hexable | number, options?: DataOptions): Uint8Array;

Usage Examples:

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

// Convert hex string to bytes
const fromHex = arrayify("0x1234abcd");
// Result: Uint8Array([0x12, 0x34, 0xab, 0xcd])

// Convert number to bytes (big-endian)
const fromNumber = arrayify(0x1234);
// Result: Uint8Array([0x12, 0x34])

// Convert existing bytes array
const fromBytes = arrayify([18, 52, 171, 205]);
// Result: Uint8Array([18, 52, 171, 205])

// With options - allow missing 0x prefix
const withOptions = arrayify("1234", { allowMissingPrefix: true });
// Result: Uint8Array([0x12, 0x34])

// Handle odd-length hex with padding
const oddHex = arrayify("0x123", { hexPad: "left" });
// Result: Uint8Array([0x01, 0x23]) - padded left

const oddHexRight = arrayify("0x123", { hexPad: "right" });
// Result: Uint8Array([0x12, 0x30]) - padded right

Hexlify - Convert to Hex String

Converts various input formats to a hexadecimal string representation.

/**
 * Convert BytesLike, Hexable, number, or bigint to hex string
 * @param value - Value to convert
 * @param options - Optional conversion options
 * @returns Hex string representation (always prefixed with "0x")
 */
function hexlify(value: BytesLike | Hexable | number | bigint, options?: DataOptions): string;

Usage Examples:

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

// Convert bytes to hex string
const fromBytes = hexlify([18, 52, 171, 205]);
// Result: "0x1234abcd"

// Convert number to hex
const fromNumber = hexlify(4660);
// Result: "0x1234"

// Convert bigint to hex
const fromBigInt = hexlify(123456789012345678901234567890n);
// Result: "0x18ee90ff6c373e0ee4e3f0ad2" (or similar large hex)

// Convert existing hex string (normalizes format)
const fromHex = hexlify("0x1234ABCD");
// Result: "0x1234abcd" - normalized to lowercase

// With options - allow missing 0x prefix
const withOptions = hexlify("1234", { allowMissingPrefix: true });
// Result: "0x1234"

// Handle odd-length hex with padding
const oddHex = hexlify("0x123", { hexPad: "left" });
// Result: "0x0123" - padded left

Types

interface DataOptions {
  /** Allow hex strings without "0x" prefix */
  allowMissingPrefix?: boolean;
  /** Padding direction for odd-length hex strings */
  hexPad?: "left" | "right" | null;
}

interface Hexable {
  /** Method to convert object to hex string */
  toHexString(): string;
}