Skip to main content

Try it Live

Run Hex examples in the interactive playground

Overview

Hex provides type-safe hex string primitives for Ethereum development. Unlike byte-array primitives, Hex values are branded 0x${string} types that preserve the string representation while providing compile-time type safety.
WASM performance note: Hex string conversions and lightweight helpers (toBytes, toNumber, toBigInt, toBoolean, concat/slice/pad, and toHex() from other primitives) rarely benefit from WASM. The JS↔WASM call overhead typically outweighs the work, and JavaScript built-ins for string/number formatting are already very fast. Prefer the default JS entrypoint for these operations; use WASM for compute-heavy tasks like Keccak256 hashing, ABI/RLP encoding/decoding, or elliptic-curve crypto.Voltaire’s WASM implementations optimize for a balance of performance and bundle size. If you need maximum performance, build from source with the performance-optimized WASM target. See /dev/build-system#typescript-targets and /dev/wasm#build-modes.
Hex is a branded template literal type. TypeScript enforces type safety through a unique Symbol brand, preventing accidental mixing with regular strings while maintaining zero runtime overhead.

Quick Start

API Methods

Constructors

Conversions

Validation

Manipulation

Comparison


Constructors

Hex Constructor

Create a hex value from a string or bytes.

fromBytes

Convert a Uint8Array to hex string.

fromNumber

Convert a number to hex with optional size padding.
Numbers must be non-negative safe integers. For values larger than Number.MAX_SAFE_INTEGER, use Hex.fromBigInt().

fromBigInt

Convert a bigint to hex with optional size padding.

fromBoolean

Convert a boolean to single-byte hex.

random

Generate cryptographically random hex of specified size.

zero

Create zero-filled hex of specified size.

Conversions

toBytes

Convert hex to Uint8Array.
Throws InvalidFormatError if missing 0x prefix or contains invalid characters. Throws InvalidLengthError if hex has odd number of digits.

toNumber

Convert hex to JavaScript number.

toBigInt

Convert hex to bigint.

toBoolean

Convert hex to boolean.

Validation

isHex

Check if a string is valid hex format.

validate

Validate and return typed hex, throws on invalid input.

isSized

Check if hex has specific byte size (type guard).

assertSize

Assert hex has specific byte size, returns sized type.

Manipulation

concat

Concatenate multiple hex strings.

slice

Slice hex by byte indices.

pad

Left-pad hex to target byte size.

padRight

Right-pad hex to target byte size.

trim

Remove leading zero bytes.

xor

XOR two hex strings of same length.

clone

Create a copy of a hex string.

Comparison

equals

Check equality (case-insensitive).

size

Get byte size of hex.

Types

Base branded hex type. Any length, validated at creation.

Error Handling

Hex operations throw typed errors for invalid inputs. All errors extend base classes from @tevm/voltaire/errors:

Error Types

Tree-Shaking

Import only what you need for optimal bundle size:
Importing from @tevm/voltaire/Hex instead of the main entry point enables tree-shaking. Functions like xor, random, and numeric conversions are excluded if unused.
  • Hex (Effect) - Effect.ts integration with Schema validation
  • Address - 20-byte Ethereum addresses (uses Hex internally)
  • Bytes - Fixed-size byte arrays
  • Keccak256 - Hash functions that produce hex output

Specification References