Skip to main content
Convert between hex strings and byte arrays using Tevm’s Hex primitive
import { Hex } from '@tevm/voltaire/Hex';

// Encode bytes to hex string
const bytes = new Uint8Array([72, 101, 108, 108, 111]); // "Hello" in bytes
const hexString = Hex(bytes);
console.log('Hex from bytes:', hexString);

// Decode hex string back to bytes
const decoded = Hex.toBytes(hexString);
console.log('Bytes from hex:', decoded);

// Create hex directly from string
const directHex = Hex('0x48656c6c6f');
console.log('Direct hex:', directHex);
This is a fully executable example. View the complete source with test assertions at examples/hex-and-bytes/hex-encode-decode.ts.