Overview
Bytes32 is a generic 32-byte data structure used throughout Ethereum. While Hash is specifically for cryptographic hashes, Bytes32 is the general-purpose type for any 32-byte data like storage values and numeric representations.Bytes32 is a Zig-only primitive. For TypeScript, use the branded types like Hash, BlockHash, or Uint256.
Type Definition (Zig)
Creating Bytes32
fromBytes
Create from raw bytes (must be exactly 32 bytes).fromHex
Parse from hex string (with or without 0x prefix).InvalidBytes32Length- Not exactly 32 bytesInvalidHexCharacter- Invalid hex character
fromNumber
Create from u64 (big-endian, zero-padded left).fromBigint
Create from u256 (big-endian).zero
Create all-zeros Bytes32.Converting Bytes32
toBytes
Get raw bytes slice.toHex
Convert to hex string with 0x prefix (requires allocator).toBigint
Convert to u256 (big-endian).toHash
Semantic conversion to Hash type.toAddress
Extract last 20 bytes as Address.Comparison
equals
Compare two Bytes32 for byte-equality.compare
Lexicographic comparison. Returns -1, 0, or 1.isZero
Check if all bytes are zero.Utilities
size
Always returns 32.clone
Create independent copy.Roundtrip Examples
See Also
- Bytes32 (Effect) - Effect.ts integration with Schema validation
- Hash - Cryptographic hash type
- Address - 20-byte address type
- Uint256 - 256-bit unsigned integer

