Skip to main content

Try it Live

Run Address examples in the interactive playground
This page is a placeholder. All examples on this page are currently AI-generated and are not correct. This documentation will be completed in the future with accurate, tested examples.
View the complete executable example at playground/src/examples/primitives/address/to-hex.ts.
WASM performance note: toHex() is a simple conversion that generally does not run faster under WASM. The JS↔WASM boundary adds overhead, while JavaScript’s built-ins already handle hex/string formatting efficiently. Prefer the default JS entrypoint for toHex() and similar conversions; use WASM for compute-heavy operations (Keccak256 hashing, ABI/RLP encoding/decoding, secp256k1/BLS) where performance gains are significant.Voltaire’s WASM builds intentionally optimize for both performance and bundle size. For maximum performance, build from source with the performance-optimized WASM target. See /dev/build-system#typescript-targets and /dev/wasm#build-modes.

primitives_address_to_hex(address: *const PrimitivesAddress, buf: [*]u8): c_int

Convert address to hex string with 0x prefix. Buffer must be at least 42 bytes.Parameters:
  • address: *const PrimitivesAddress - Address to convert (20 bytes)
  • buf: [*]u8 - Output buffer for hex string (must be at least 42 bytes)
Returns: c_int - PRIMITIVES_SUCCESS (0) on successExample:
#include <tevm/primitives.h>
#include <stdio.h>

PrimitivesAddress addr = { .bytes = {
    0x74, 0x2d, 0x35, 0xcc, 0x66, 0x34, 0xc0, 0x53,
    0x29, 0x25, 0xa3, 0xb8, 0x44, 0xbc, 0x9e, 0x75,
    0x95, 0xf5, 0x1e, 0x3e
}};

char hex_buf[42];
primitives_address_to_hex(&addr, hex_buf);

printf("%.*s\n", 42, hex_buf);
// "0x742d35cc6634c0532925a3b844bc9e7595f51e3e"
Defined in: c_api.zig:55

Format

Output format: 0x + 40 lowercase hexadecimal characters Total length: 42 characters Example outputs:
  • 0x0000000000000000000000000000000000000000 (zero address)
  • 0x742d35cc6634c0532925a3b844bc9e7595f51e3e
  • 0xffffffffffffffffffffffffffffffffffffffff (max address)

Case Sensitivity

toHex() always returns lowercase, regardless of input format:
import { Address } from '@tevm/voltaire'

// Mixed case input (EIP-55 checksummed)
const checksummed = Address("0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed")
console.log(checksummed.toHex())
// "0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed" (lowercase)

// Uppercase input
const uppercase = Address("0x742D35CC6634C0532925A3B844BC9E7595F51E3E")
console.log(uppercase.toHex())
// "0x742d35cc6634c0532925a3b844bc9e7595f51e3e" (lowercase)
For checksummed output, use toChecksummed() instead.

Use Cases

Display and Logging

import { Address } from '@tevm/voltaire'

const addr = Address(privateKey)
console.log(`Address: ${addr.toHex()}`)

Storage and Serialization

import { Address } from '@tevm/voltaire'

interface Account {
  address: string
  balance: bigint
}

const account: Account = {
  address: addr.toHex(),
  balance: 1000000n
}

// Serialize to JSON
const json = JSON.stringify(account)

Comparison and Hashing

import { Address } from '@tevm/voltaire'

// Normalize for comparison
const normalized1 = addr1.toHex()
const normalized2 = addr2.toHex()

if (normalized1 === normalized2) {
  console.log("Addresses match")
}

// Use as Map/Set keys
const balances = new Map<string, bigint>()
balances.set(addr.toHex(), 1000n)

Database Queries

import { Address } from '@tevm/voltaire'

async function getBalance(address: Address) {
  const hex = address.toHex()
  return db.query('SELECT balance FROM accounts WHERE address = ?', [hex])
}

Performance

Zero allocation - Creates new string but no intermediate buffers. Time complexity: O(n) where n = 20 bytes (constant time). String concatenation: Efficient for this fixed size (40 hex chars). For repeated conversions, consider caching the result:
import { Address } from '@tevm/voltaire'

class AddressWrapper {
  private _hexCache?: string

  constructor(private address: Address) {}

  toHex(): string {
    if (!this._hexCache) {
      this._hexCache = this.address.toHex()
    }
    return this._hexCache
  }
}

Comparison with Other Formats

import { Address } from '@tevm/voltaire'

const addr = Address("0x742d35Cc6634C0532925a3b844Bc9e7595f51e3e")

// Lowercase (toHex)
console.log(addr.toHex())
// "0x742d35cc6634c0532925a3b844bc9e7595f51e3e"

// Checksummed (toChecksummed)
console.log(addr.toChecksummed())
// "0x742d35Cc6634C0532925a3b844Bc9e7595f51e3e"

// Shortened (toShortHex)
console.log(addr.toShortHex())
// "0x742d35...1e3e"

See Also