How to Read a Raw Script in Hex
1. How to Read a Raw Script in Hex
Introduction
Every Bitcoin transaction output carries a locking script (scriptPubKey), and every transaction input carries an unlocking script (scriptSig). These scripts are serialized as raw bytes and typically presented in hexadecimal form when you inspect raw transactions. Understanding how to read these hex sequences is the foundational skill for all further script debugging and analysis work.
When you request a raw transaction from the Bitcoin network or Bitcoin Core, the scripts appear as continuous hex strings. Without knowing the encoding rules, these strings look like random noise. Once you understand the structure, however, you can manually parse any script by hand.
The Encoding Rules
Bitcoin scripts are sequences of opcodes and data push elements. The encoding works as follows:
-
Each opcode is exactly one byte (two hex characters).
-
Data pushes consist of a length prefix byte followed by the actual data bytes.
-
Opcodes in the range
0x01–0x4b(1–75 decimal) directly indicate that the next N bytes are data to be pushed onto the stack. -
OP_PUSHDATA1(0x4c) means the next 1 byte encodes the data length. -
OP_PUSHDATA2(0x4d) means the next 2 bytes (little-endian) encode the data length. -
OP_PUSHDATA4(0x4e) means the next 4 bytes (little-endian) encode the data length.
Full Opcode Reference Table (Selected)
| Hex | Decimal | Opcode Name | Description | |
Pro Tip
When debugging scripts, always start with a high-level disassembly before diving into the stack trace. Tools like bitcoin-cli decodescript are your first line of defense in identifying standard script patterns.
TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation: