Magic Bytes
The message delimiter on the bitcoin network.
Magic bytes are used as a way to identify the separate messages sent between nodes on the bitcoin network.
For example, if you are trying to connect to a node with your own code, every message you send to that node should begin with f9beb4d9
, and every message you receive from that node will start with the same magic bytes too.
The magic bytes.
Magic bytes are 4 bytes in length, and will be different depending on which network you’re on:
Network | Magic Bytes |
---|---|
Mainnet | f9beb4d9 |
Testnet3 | 0b110907 |
Regtest | fabfb5da |
Example.
Here’s a version
message I received from a node on the Testnet3 network:
0b11090776657273696f6e000000000066000000c0094f817e1101000d000000000000004659775800000000000000000000000000000000000000000000ffff0000000000000d000000000000000000000000000000000000000000000000003d2324b2fc764108102f5361746f7368693a302e31332e312fab3d100001
And here’s a tx
message:
0b110907747800000000000000000000e1000000db31994501000000018ad494471addef205294beb3b2673e2734a0c558b8dbc1334412e42b4a730b32010000006a473044022060c7048255d32a3c8014775d93e32339ed9f460c1f33770fab14444af2cdfb5f02204bd6020742b5deda36712c55f5110a2f4d9ea6c97e45a90bca39d134932c91b2012103b93183cf139818b023f79d6b9dc0c9b80276df9f188948e587db80c988337ec7ffffffff0280d1f008000000001976a9141f81f255c1df8d1b7665e7e7340b893ede2301a988acb8665b00000000001976a9149d28b845d29c1237e7273df9108f1597d4939e0688ac00000000
Magic bytes are also used to separate block data in the blk*.dat files.
Why use Magic Bytes?
If you connect to a bitcoin node, think of the messages you get from it as a continual stream of data.

f9beb4d9
’s help you spot when a new message starts.https://github.com/in3rsha/php-simple-bitcoin-node
If you are trying to read this data, it’s good to have a reliable way of knowing when a new message starts (and ends). This is why a specific set of “magic bytes” are used as a “marker” so that you can always identify the start of a new message.
So there’s nothing actually magical about magic bytes – it’s just a way of segmenting data.
Why these bytes in particular?
These bytes have no specific meaning, except for:
The message start string is designed to be unlikely to occur in normal data. The characters are rarely used upper ASCII, not valid as UTF-8, and produce a large 32-bit integer with any alignment. – chainparams.cpp
So they could be different, but these are just 4 bytes that have the properties that make for good magic bytes on the bitcoin network.