Appendices Appendix C

Hex Parsing Cheat Sheet

All integers are encoded in little-endian.—Bitcoin Protocol Documentation

This appendix is a quick-reference guide for reading raw Bitcoin transactions byte by byte. Print it, laminate it, and keep it next to your hex editor.

C.1VarInt (CompactSize) Encoding

Bitcoin uses a variable-length integer encoding for counts and lengths:

First ByteRangeTotal BytesEncoding
0x000xfc0–2521Value is the byte itself
0xfd253–65,5353fd + 2 bytes (LE)
0xfe65,536–\(2^{32}-1\)5fe + 4 bytes (LE)
0xff\(2^{32}\)–\(2^{64}-1\)9ff + 8 bytes (LE)

C.2Legacy Transaction Layout

OffsetSizeFieldNotes
04 bytesnVersionLittle-endian. Usually 01000000 (v1) or 02000000 (v2).
4varintvin_countNumber of inputs.
+032 bytesprev_txidInternal byte order (reversed from display).
+324 bytesprev_voutOutput index, little-endian.
+36varintscriptSig_lenLength of scriptSig in bytes.
+37variesscriptSigUnlocking script.
4 bytesnSequenceUsually ffffffff or fdffffff.
varintvout_countNumber of outputs.
+08 bytesvalueSatoshis, little-endian.
+8varintscriptPubKey_lenLength of scriptPubKey.
+9variesscriptPubKeyLocking script.
4 bytesnLockTimeLittle-endian. Block height or Unix time.

C.3SegWit Transaction Layout

SegWit transactions insert a marker and flag after the version, and append witness data before the locktime:

OffsetSizeFieldNotes
04 bytesnVersionSame as legacy.
41 bytemarkerAlways 00. Signals SegWit serialization.
51 byteflagAlways 01.
6varintvin_countNumber of inputs.
varintvout_countNumber of outputs.
varintitem_countNumber of witness items for this input.
varintitem_lenLength of witness item.
variesitem_dataWitness item (signature, pubkey, script, etc.).
4 bytesnLockTimeAlways the last 4 bytes of the raw transaction.

nVersion, prev_vout, nSequence, value, and nLockTime are all little-endian. The prev_txid is stored in internal byte order (reversed from the display hash shown on block explorers). When you see txid abcd...1234 on mempool.space, the raw bytes are 3412...cdab.

C.4Common ScriptPubKey Patterns

Recognizing the locking script tells you the output type instantly:

Pattern (hex)TypeChapter
41 <65B pubkey> acP2PK (uncompressed)Ch. 4
21 <33B pubkey> acP2PK (compressed)Ch. 4
76 a9 14 <20B hash> 88 acP2PKHCh. 5
a9 14 <20B hash> 87P2SHCh. 6
00 14 <20B hash>P2WPKH (native SegWit v0)Ch. 9
00 20 <32B hash>P2WSH (native SegWit v0)Ch. 10
51 20 <32B key>P2TR (SegWit v1)Ch. 12
6a <data>OP_RETURN (unspendable)Ch. 15
51 02 4e73P2A (Pay-to-Anchor)Ch. 17

C.5Common Byte Sequences

BytesMeaningContext
01000000Version 1Pre-BIP 68 transactions
02000000Version 2Enables relative timelocks (BIP 68)
03000000Version 3 (TRUC)BIP 431 transaction relay policy
0001SegWit marker+flagImmediately after version in SegWit txs
ffffffffnSequence maxDisables RBF; disables BIP 68
fdffffffnSequence \(-2\)RBF signal; BIP 68 disabled
feffffffnSequence \(-1\)RBF signal; BIP 68 disabled
00000000nLockTime 0No absolute timelock
ffffffff… (32 bytes)Null prevout hashCoinbase transaction input
ffffffff (4 bytes)Coinbase vout indexAlways 0xFFFFFFFF for coinbase

C.6Signature Encoding (DER)

ECDSA signatures in legacy/SegWit v0 use DER encoding:

OffsetFieldNotes
030Compound structure tag
1lengthTotal remaining bytes
202Integer tag for \(r\)
3\(r\)-lengthUsually 32 or 33 bytes
4\(r\)-valuePrepend 00 if high bit set
02Integer tag for \(s\)
\(s\)-lengthUsually 32 bytes (low-\(s\) normalized)
\(s\)-valueBIP 146 requires \(s \leq n/2\)
lastsighash byte01=ALL, 02=NONE, 03=SINGLE, 81=ANYONECANPAY

C.7Sighash Types

ValueNameWhat It Signs
0x01SIGHASH_ALLAll inputs and all outputs (default)
0x02SIGHASH_NONEAll inputs, no outputs
0x03SIGHASH_SINGLEAll inputs, only the output at same index
0x81ALL|ANYONECANPAYOnly this input, all outputs
0x82NONE|ANYONECANPAYOnly this input, no outputs
0x83SINGLE|ANYONECANPAYOnly this input, matching output
Reading Raw Hex

Practice routine: Take any txid from mempool.space, click "Raw Hex," and parse it field by field using this cheat sheet. Start with the version bytes, count the inputs, read each prevout, decode the scriptSig (or note its absence for SegWit), count the outputs, decode each value and scriptPubKey, parse the witness stack if present, and finish with the locktime. After five transactions, you will read hex fluently.

Every byte tells a story. Now you know how to read it.

← Previous Next →