Try it Live
Run Address examples in the interactive playground
- C
primitives_address_to_bytes(PrimitivesAddress address, uint8_t* out_bytes): void
Convert address to byte array in C. Copies 20 bytes to provided buffer.Parameters:address: PrimitivesAddress- Address to convertout_bytes: uint8_t*- Buffer for output bytes (must be at least 20 bytes)
void - Bytes written to out_bytesExample:- No allocation - copies to provided buffer
- Caller must ensure buffer is at least 20 bytes
- Safe to use stack-allocated buffer
PrimitivesAddress is a struct containing a 20-byte array, you can access bytes directly:Memory Behavior
TypeScript/JavaScript:- Returns a copy of the underlying bytes
- Safe to modify returned array without affecting original
- No aliasing between address and returned bytes
- Direct access to fixed-size array
- Copying requires explicit
@memcpy - Stack-allocated by default
Use Cases
Binary Protocols
Serialize addresses for binary protocols:Hash Computation
Include address in hash calculations:Database Storage
Store addresses as binary blobs:Network Transmission
Send addresses over WebSocket or other binary protocols:ABI Encoding
Convert to bytes for ABI encoding:Direct Access
SinceAddressType is a Uint8Array, you can access bytes directly:
toBytes():
- Need a mutable copy
- Want to emphasize byte conversion
- Passing to APIs expecting Uint8Array (not branded type)
- Reading bytes without modification
- Iterating over bytes
- Using array methods
Performance
Memory: Creates new Uint8Array (20 byte allocation) Time complexity: O(n) where n = 20 (constant time) Copy overhead: Minimal for 20 bytes For performance-critical code, access bytes directly instead:Comparison with Other Conversions
See Also
- fromBytes - Create from Uint8Array
- toHex - Convert to hex string
- toAbiEncoded - Convert to 32-byte ABI encoding
- AddressType - Type-safe Uint8Array wrapper

