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 branded0x${string} types that preserve the string representation while providing compile-time type safety.
- Type Definition
Quick Start
- Basic Usage
- Numeric Conversions
- Manipulation
API Methods
Constructors
Hex(value)- Create from string or bytesfromBytes(bytes)- Convert Uint8Array to hexfromNumber(value, size?)- Convert number to hexfromBigInt(value, size?)- Convert bigint to hexfromString(value)- Create from raw stringfromBoolean(value)- Convert boolean to hexrandom(size)- Generate random hexzero(size)- Create zero-filled hex
Conversions
toBytes(hex)- Convert to Uint8ArraytoNumber(hex)- Convert to numbertoBigInt(hex)- Convert to biginttoString(hex)- Get string representationtoBoolean(hex)- Convert to boolean
Validation
isHex(value)- Check if valid hex stringvalidate(value)- Validate and return typed hexisSized(hex, size)- Check byte sizeassertSize(hex, size)- Assert byte size
Manipulation
concat(...hexes)- Concatenate hex stringsslice(hex, start, end?)- Slice by byte indexpad(hex, size)- Left-pad with zerospadRight(hex, size)- Right-pad with zerostrim(hex)- Remove leading zerosxor(hex, other)- XOR two hex stringsclone(hex)- Create a copy
Comparison
equals(a, b)- Check equality (case-insensitive)size(hex)- Get byte size
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.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.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
- HexType
- Sized
- Bytes
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:Related
- 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
- Ethereum Hex Encoding - Hex format in Ethereum
- JSON-RPC Hex - Hex string conventions in JSON-RPC

