In this track: ABI encoding foundations
advanced · 4 MIN READ

Encode bytes4 versus bytes32

Fixed bytes are right-padded, unlike unsigned integers.

LET’S PICTURE IT

A label occupying the front of a fixed-width field leaves blank space after it.

SEE IT FOR YOURSELF
Build a transfer call from typed fieldsStep 1 / 4
Build a transfer call from typed fieldstransfer(address,uint256)Recipient: 0x0000…0001Raw amount: 100Diagram labels abbreviate addresses; the lab shows full bytes4-byte selectora9059cbbEach box is one byte; this prefix is not a 32-byte wordWord 0 · recipient address12 zero bytes20 address bytes000…0001Left padding preserves the lower-order address valueSelector4 bytesa9059cbbAddress32 bytesword 0Amount32 bytesword 1 → 0x644 + 32 + 32 = 68 bytesThe integer does not contain token decimals or market price

The standard transfer interface takes an address and a raw integer amount. This diagram uses address one and amount one hundred.

Animated local teaching model. No wallet, RPC or live blockchain data. Some labels and hashes are abbreviated for readability.

Use the declared fixed size

bytes4 contains exactly four bytes; bytes32 contains exactly thirty-two. A value of the wrong declared length needs validation rather than an assumption about how the author meant to pad it.

Pad the standard word on the right

bytes4 0x12345678 begins the ABI word with those four bytes, then twenty-eight zero bytes follow. A uint32 with the same bits would appear at the other end of its word.

Do not confuse dynamic bytes

The dynamic bytes type uses an offset-and-tail layout when placed in an argument tuple. It is not simply bytes32 with a variable amount of trimming.

Inspect selectors carefully

A selector can be described as bytes4 in application logic, but the selector prefix of function calldata is only four actual bytes. It is not automatically a full encoded bytes4 argument word.

Follow the bytes

types: bytes4
values: ["0x12345678"]
no function selector prefix

word 0 · argument offset 0 bytes
0x1234567800000000000000000000000000000000000000000000000000000000

Compare this alignment with a uint32 encoding of the same numeric bits.

TURN UNDERSTANDING INTO PRACTICEOpen the ABI encoder →
QUICK CHECK · NO PRESSURE

Does bytes4 0x12345678 appear at the start or end of its standard ABI word?

Your progress stays in this browser.
Keep exploring the original referenceSolidity documentation ↗

Original explanations by EtherTools. Examples are educational and source-linked; this site is independent of the Ethereum Foundation. Our approach.