Tool · Bytes

CompactSize

How a count is written in 1, 3, 5 or 9 bytes

CompactSize

How Pearl writes a count in 1, 3, 5 or 9 bytes

Encoded 3 bytes
fdprefix 0302 number, little-endian

0xfd says “the number is in the next 2 bytes”. Read 03 02 backwards (little-endian): 0x0203 = 515.

First byte Then Numbers Size
00 – fc nothing: the byte is the number 0 – 252 1 B
fd 2 bytes, little-endian 253 – 65,535 3 B
fe 4 bytes, little-endian 65,536 – 4,294,967,295 5 B
ff 8 bytes, little-endian 4,294,967,296 – 2⁶⁴ − 1 9 B
Runs in your browser · nothing is sent src/lib/bytes.ts · same rule as Bitcoin

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 02 is 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 00 spells 252, which fits in one byte, so it must be written fc. 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.

Suggested

Up and down arrows move through the results, Enter opens one, Escape closes. Anywhere on the site, slash or Command K opens this search, and the square brackets [ and ] go to the previous and next page.