Calculating Hash Endianness
Calculating the Hash: The Endianness Challenge
Calculating a Bitcoin block hash is mathematically simple but technically tricky due to Endianness. Bitcoin internally processes data in Little-Endian, but humans and block explorers display data in Big-Endian.
1. Internal Representation (Little-Endian)
Inside the 80-byte header, every field (except the Merkle Root and Prev Hash, which are already hashes) is stored in little-endian byte order.
-
When a node hashes the header, it treats it as a raw blob of 80 bytes.
-
The output of the double-SHA256 is also a 32-byte blob.
2. Display Representation (Big-Endian)
When you see a block hash like 000000000000000000052d31... on a website, it has been reversed.
-
In the raw computer memory, those leading zeroes are actually at the end of the 32-byte string.
-
The first non-zero bytes you see in the explorer are actually the last bytes in the raw data.
3. The 32-Byte Swap
To convert a raw internal block hash to a human-readable "Block ID":
-
Take the 32-byte output of the double-SHA256.
-
Reverse the order of the bytes (Byte 31 becomes Byte 0, etc.).
-
Convert the reversed bytes to a hexadecimal string.
4. Why the Confusion?
This discrepancy exists because SHA-256 is a "big-endian" algorithm by design (as defined in FIPS 180-4). However, the x86 processors that Bitcoin was originally developed on are "little-endian." Satoshi Nakamoto's original implementation handled these conversions in a way that resulted in the reversed display format we use today.
| Format | Example Start | Used By |
|---|---|---|
| Internal (Raw) | 6a 64 04 d6... |
CPU, Hashing, Mining |
| Display (Hex) | 00000000... |
Block Explorers, Users |
[!TIP] If you are writing a script to verify block hashes, always remember to reverse the byte order before comparing your result to a value from a block explorer.
Next, we will look at the Leading Zeroes Requirement and the probability of mining.
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: