Skip to main content

Try it Live

Run Address examples in the interactive playground

char* address_to_short_hex(address_t address, uint8_t prefix_len, uint8_t suffix_len, allocator_t allocator)

Format address as shortened hex string with ellipsis. Caller must free returned string.Parameters:
  • address: address_t - Address to format (20 bytes)
  • prefix_len: uint8_t - Characters after 0x to show
  • suffix_len: uint8_t - Characters at end to show
  • allocator: allocator_t - Memory allocator for result string
Returns: char* - Shortened hex string (must be freed by caller)Example:
#include <tevm/primitives.h>
#include <stdio.h>
#include <stdlib.h>

address_t addr = address_from_hex("0x742d35Cc6634C0532925a3b844Bc9e7595f51e3e", allocator);

// Default: 6 + 4
char* short_hex = address_to_short_hex(addr, 6, 4, allocator);
printf("%s\n", short_hex);  // "0x742d35...1e3e"
free(short_hex);

// Custom lengths: 8 + 6
char* custom = address_to_short_hex(addr, 8, 6, allocator);
printf("%s\n", custom);  // "0x742d35Cc...f51e3e"
free(custom);

// Minimal: 4 + 4
char* minimal = address_to_short_hex(addr, 4, 4, allocator);
printf("%s\n", minimal);  // "0x742d...1e3e"
free(minimal);

// Full address if prefix + suffix >= 40
char* full = address_to_short_hex(addr, 20, 20, allocator);
printf("%s\n", full);  // Full checksummed address
free(full);
Memory Management:
  • Result string allocated by provided allocator
  • Caller responsible for freeing returned string
  • Length: 2 + prefix_len + 3 + suffix_len + 1 bytes (including null terminator)
Defined in: primitives.h

Format

Output pattern: 0x{prefix}...{suffix} Default lengths: prefix=6, suffix=4 Total length: 2 + prefix + 3 + suffix characters Example outputs:
  • "0x742d35...1e3e" (default: 6+4 = 15 chars total)
  • "0x742d35Cc...f51e3e" (custom: 8+6 = 19 chars total)
  • "0x0000...0000" (zero address)

Use Cases

UI Display

Show addresses in constrained spaces:
import { Address } from 'tevm'

function AddressDisplay({ address }: { address: Address }) {
  return (
    <div className="address-container">
      <span className="address-short" title={address.toHex()}>
        {address.toShortHex()}
      </span>
    </div>
  )
}

Mobile Interfaces

Optimize for small screens:
import { Address } from 'tevm'

function MobileAddressCard({ address }: { address: Address }) {
  const isMobile = window.innerWidth < 768

  return (
    <div>
      {isMobile
        ? address.toShortHex(4, 4)   // "0x742d...1e3e"
        : address.toChecksummed()     // Full address
      }
    </div>
  )
}

Transaction Lists

Display sender/recipient in transaction tables:
import { Address } from 'tevm'

interface Transaction {
  from: Address
  to: Address
  value: bigint
}

function TransactionRow({ tx }: { tx: Transaction }) {
  return (
    <tr>
      <td title={tx.from.toHex()}>{tx.from.toShortHex()}</td>
      <td>→</td>
      <td title={tx.to.toHex()}>{tx.to.toShortHex()}</td>
      <td>{tx.value.toString()}</td>
    </tr>
  )
}

Notifications

Show addresses in toast notifications:
import { Address } from 'tevm'

function showTransactionSent(to: Address, amount: bigint) {
  toast.success(
    `Sent ${amount} to ${to.toShortHex()}`,
    { duration: 5000 }
  )
}

Length Constraints

If prefixLength + suffixLength >= 40, returns full address:
import { Address } from 'tevm'

const addr = Address("0x742d35Cc6634C0532925a3b844Bc9e7595f51e3e")

// Requested lengths exceed total
console.log(addr.toShortHex(25, 20))
// "0x742d35cc6634c0532925a3b844bc9e7595f51e3e" (full address)

// Exact length (40)
console.log(addr.toShortHex(20, 20))
// "0x742d35cc6634c0532925a3b844bc9e7595f51e3e" (full address)

Visual Comparison

import { Address } from 'tevm'

const addr = Address("0x742d35Cc6634C0532925a3b844Bc9e7595f51e3e")

// Different lengths
console.log(addr.toShortHex(4, 4))   // "0x742d...1e3e"
console.log(addr.toShortHex(6, 4))   // "0x742d35...1e3e"  (default)
console.log(addr.toShortHex(8, 6))   // "0x742d35Cc...f51e3e"
console.log(addr.toShortHex(10, 8))  // "0x742d35Cc66...95f51e3e"

// Full address
console.log(addr.toHex())            // "0x742d35cc6634c0532925a3b844bc9e7595f51e3e"
console.log(addr.toChecksummed())    // "0x742d35Cc6634C0532925a3b844Bc9e7595f51e3e"

Best Practices

Always provide full address in tooltip/title:
<span title={address.toHex()}>
  {address.toShortHex()}
</span>
Use consistent lengths across your UI:
// Define once, use everywhere
const SHORT_ADDR_FORMAT = { prefix: 6, suffix: 4 }

function formatAddress(addr: Address): string {
  return addr.toShortHex(SHORT_ADDR_FORMAT.prefix, SHORT_ADDR_FORMAT.suffix)
}
Consider context when choosing lengths:
  • Mobile: 4+4 (minimal)
  • Desktop lists: 6+4 (default)
  • Detailed views: 8+6 or full
  • Copy operations: Always use full address

Accessibility

Ensure screen readers get full address:
import { Address } from 'tevm'

function AccessibleAddress({ address }: { address: Address }) {
  const full = address.toChecksummed()
  const short = address.toShortHex()

  return (
    <span aria-label={full}>
      <span aria-hidden="true">{short}</span>
    </span>
  )
}

Performance

No cryptographic operations - Simple string slicing. Time complexity: O(1) constant time. Memory: Creates single new string. Very efficient for display purposes.

See Also