Skip to main content

Try it Live

Run Bytes examples in the interactive playground
Most Important Bytes Type - Bytes32 is THE fundamental 256-bit type in Ethereum. Used everywhere: storage slots, hashes, merkle trees, and generic 32-byte values.

Overview

Bytes32 is a branded Uint8Array specialization of the generic Bytes<N> type, representing exactly 32 bytes (256 bits). It’s the most common fixed-size type in Ethereum and provides strict type safety for 256-bit values through Symbol-based branding. See the Bytes overview for generic Bytes<N> documentation and other size specializations.

Why Bytes32 is Critical

Ethereum’s fundamental 256-bit type:
  • Storage slots - EVM storage uses 32-byte slots
  • Hashes - Keccak256 outputs 32 bytes
  • Merkle trees - Node values are 32 bytes
  • Generic values - Most fixed-size data is 32 bytes
  • ABI encoding - Uint256, bytes32, etc. use 32 bytes

Difference from Keccak256Hash

Both are 32 bytes, but semantic meaning differs:
When to use each:
  • Keccak256Hash - Cryptographic hash outputs (keccak256 results)
  • Bytes32 - Storage slots, generic 32-byte values, numeric conversions

Type Safety

Bytes32 is a specialization of Bytes<32> using Symbol-based branding for compile-time type safety with zero runtime overhead:
The brand symbol is a unique TypeScript symbol that creates nominal typing - structurally identical types are incompatible if they have different brands:
This branding system provides:
  • Compile-time safety - Cannot mix different sized Bytes types
  • Zero runtime cost - Brand only exists in TypeScript, erased after compilation
  • Hardcoded length - Type includes { readonly size: 32 } for static length checking

Hex String Representation

For hex string representations, use the HexBytes32 type which extends branded hex strings with size information:

Case Sensitivity

HexBytes32 supports case variants through additional branding:
Most Ethereum tooling uses lowercase hex by default, but uppercase is common in certain contexts (like BLS signatures).

Constructors

from

Universal constructor accepting multiple types:

fromHex

From hex string with strict validation:

fromBytes

From Uint8Array with size check:

fromNumber

From number with padding (big-endian):

fromBigint

From bigint with padding (big-endian):

zero

Create zero-filled Bytes32:

Conversions

toHex

Convert to hex string:

toUint8Array

Convert to raw bytes:

toBigint

Convert to bigint (big-endian):

toKeccak256Hash

Convert to Keccak256Hash (semantic change):

toAddress

Extract address from last 20 bytes:

Operations

equals

Check equality:

compare

Compare two values:

clone

Create independent copy:

isZero

Check if all zeros:

size

Get size (always 32):

Constants

SIZE

Bytes32 size in bytes:

ZERO

Zero-filled constant:

Error Handling

Bytes32 validates size strictly:

Ethereum Use Cases

Storage Slots

Merkle Trees

Generic 256-bit Values

  • Bytes - Generic Bytes<N> type documentation
  • Keccak256 - Cryptographic hashes (also 32 bytes)
  • Bytes16 - 128-bit fixed-size bytes
  • Bytes64 - 512-bit fixed-size bytes
  • Uint - 256-bit unsigned integers
  • Address - 160-bit Ethereum addresses