Skip to content
DonateБГ

Data

Hexadecimal

Hexadecimal in Bitcoin: a base-16 system with the digits 0 to 9 and A to F, used to write bytes. Learn to read and convert hex.

By Dimitar Zurnov · Updated 4 Nov 2026
Adapted from Learn Me a Bitcoin by Greg Walker.

Hexadecimal, or hex, is a number system with 16 symbols: the digits 0 to 9 and the letters A to F. Each hex digit stands for a value from 0 to 15. Bitcoin uses hex to write bytes, because two hex digits cover exactly one byte. Most of the keys, IDs and raw data you meet in Bitcoin are written in hex.

What do the hex digits mean?

The digits 0 to 9 keep their usual values. The letters stand for the numbers 10 to 15: A is 10, B is 11, C is 12, D is 13, E is 14 and F is 15. Upper and lower case mean the same thing, so 1337af and 1337AF are the same number.

How do the prefixes work?

A prefix tells you which base a number is written in. 0x means hexadecimal, 0b means binary and 0d means decimal. So 0x100 is 256, 0b100 is 4 and 0d100 is 100. A number with no prefix is decimal, so 100 means one hundred.

Bitcoin software usually prints hex without a prefix. That is why a block hash or a private key shows up as a run of characters with no 0x in front.

Why is hex used for bytes?

One byte has 256 possible values, and one hex digit has 16. Since 16 × 16 is 256, a byte needs exactly two hex digits. A 32-byte private key is therefore 64 hex digits:

c413a713f02ed032ab1f4229d3cdd78e301798f0fd5198e0d5609a99799f5dc9

Every pair of digits is one byte, so the length of a hex string tells you the size of the data. A 4-byte field is always 8 digits, and a 32-byte hash is always 64. The bytes page has the full list of sizes.

Each hex digit covers exactly four bits, which is half a byte. So you can read a byte in two steps. The byte 10110101 splits into 1011 and 0101. The first is B (11) and the second is 5, so the byte is B5.

How do you convert hex to decimal?

Multiply each digit by a power of 16, then add the results together. The rightmost digit is multiplied by 16⁰, which is 1, because the zeroth power of any number is 1. The next digit to the left is multiplied by 16¹, then 16², and so on. The diagram works through 02A13B.

The hex number 02A13B: each digit times its power of 16, added up to 172,347. 02A13B, digit by digit 0 0 × 16⁵ 0 2 2 × 16⁴ = 2 × 65,536 131,072 A 10 × 16³ = 10 × 4,096 40,960 1 1 × 16² = 1 × 256 256 3 3 × 16¹ = 3 × 16 48 B 11 × 16⁰ = 11 × 1 11 131,072 + 40,960 + 256 + 48 + 11 = 172,347
Each place in hex is a power of 16, as each place in decimal is a power of 10.

Decimal works the same way. The number 123 means 1 × 10² + 2 × 10¹ + 3 × 10⁰, which is one hundred, two tens and three ones.

You rarely need to do this by hand. Python converts a hex string in one line:

int("02A13B", 16)    # 172347

How do you convert decimal to hex?

Divide the number by 16 again and again. Each remainder, the amount left over after division, is one hex digit. The first remainder is the least significant digit. Programmers get the remainder with the % operator. Stop when the quotient reaches zero. Here is 6735:

6735 / 16 =  420, remainder 15 (F)
 420 / 16 =   26, remainder  4
  26 / 16 =    1, remainder 10 (A)
   1 / 16 =    0, remainder  1

Read the remainders from the bottom line upwards: 1, A, 4, F. So 6735 is 1A4F in hex. In Python, format(6735, "X") gives the same answer.

Where do you see hex in Bitcoin?

  • Private keys, which are 32 bytes, so 64 hex digits. The private key page explains the valid range.
  • Transaction IDs and block hashes, also 32 bytes each. Explorers show them in reverse byte order.
  • Raw transactions and block headers, which Bitcoin Core prints as one long hex string.
  • Scripts, where each byte is an opcode or a piece of data.

How do you convert in code?

# decimal to hex, no prefix: '1a4f'
format(6735, "x")
# with the 0x prefix: '0xff'
hex(255)
# hex to decimal: 6735
int("1a4f", 16)

Remember that the bytes inside a hex number are not always in the order you expect. Numbers in raw data are stored little-endian, so reverse their bytes before you read the value. The hash function page explains where the 32-byte hashes come from. For the number system in more depth, see the Wikipedia article on hexadecimal.

In this section: Data

All of Bitcoin from zero

Something look wrong? Tell us