Tool · Bytes
CompactSize
How a count is written in 1, 3, 5 or 9 bytes
What it does
Type a number to see its CompactSize bytes, or type bytes to decode them. The two boxes stay in step, and the rule table highlights which of the four forms is in use.
How to read it
- Numbers up to 252 fit in one byte: the byte is the number.
- A first byte of fd, fe or ff says “the number is in the next 2, 4 or 8 bytes”, stored
little-endian (smallest byte first). So
fd 03 02is 0x0203 = 515. - If extra bytes follow, the tool shows where the CompactSize ends. In a real transaction those bytes would be whatever it counts.
- Non-canonical forms are rejected:
fd fc 00spells 252, which fits in one byte, so it must be writtenfc. Every number has exactly one valid encoding.
Pearl specifics
Same rule as Bitcoin. Pearl uses CompactSize for the input and output counts of a transaction, script and witness lengths, witness item counts, and the transaction count of a block. Like Bitcoin’s node, pearld rejects a non-canonical CompactSize.
Under the hood
Runs entirely in your browser. Encoding and decoding are src/lib/bytes.ts (encodeCompactSize,
decodeCompactSize with the canonical check), covered by unit tests at the boundaries (0, 252, 253, 65,535,
65,536 and 2³²) and on a non-canonical input.