Skip to main content

Try it Live

Run Address examples in the interactive playground

primitives_address_from_hex(const char* hex, PrimitivesAddress* out_address): int

Create address from hex string in C. Accepts hex strings with or without 0x prefix.Parameters:
  • hex: const char* - Hex string (with or without 0x prefix)
  • out_address: PrimitivesAddress* - Pointer to output address struct (20 bytes)
Returns: int - PRIMITIVES_SUCCESS (0) on success, error code otherwiseExample:
#include "primitives.h"

// From hex string
PrimitivesAddress addr1;
int result = primitives_address_from_hex("0x742d35Cc6634C0532925a3b844Bc9e7595f51e3e", &addr1);
if (result != PRIMITIVES_SUCCESS) {
    // Handle error
}

// From hex without prefix
PrimitivesAddress addr2;
result = primitives_address_from_hex("742d35Cc6634C0532925a3b844Bc9e7595f51e3e", &addr2);

// From number (converted to hex first)
PrimitivesAddress addr3;
result = primitives_address_from_hex("0x0000000000000000000000000000000000000045", &addr3);
Error Handling:
  • Returns non-zero error code on failure
  • Check result before using out_address
  • Common errors: invalid hex format, incorrect length
Memory: No allocation - address is copied to provided struct.Defined in: primitives.h:103

AddressLike Type

AddressLike is a union type accepting any input that can be coerced to an address:
type AddressLike =
  | number
  | bigint
  | string
  | Uint8Array
  | AddressType
  | Address

Accepted Types

Number - Converted to 20-byte representation:
const addr = Address(69)
// 0x0000000000000000000000000000000000000045
Bigint - Takes lower 160 bits:
const addr = Address(0x742d35Cc6634C0532925a3b844Bc9e7595f251e3n)
String - Hex string with or without 0x prefix:
const addr1 = Address("0x742d35Cc6634C0532925a3b844Bc9e7595f51e3e")
const addr2 = Address("742d35Cc6634C0532925a3b844Bc9e7595f51e3e")
Uint8Array - Must be exactly 20 bytes:
const bytes = new Uint8Array(20)
const addr = Address(bytes)
For hex strings, always include 0x prefix to avoid ambiguity. Strings without 0x are parsed as hex but may cause confusion.

Performance

Zero-copy for Uint8Array and AddressType (no allocation). Conversion required for numbers, bigints, and strings (allocates new Uint8Array). For performance-critical code, prefer passing Uint8Array or AddressType directly.

See Also