Decimal feels natural because we learn it first, but it is just one way of writing numbers. Computers store everything in binary, and programmers use hexadecimal as a compact way to write it. Once you see the pattern, converting between them is straightforward.
Place value
In decimal, each position is worth ten times the one to its right: ones, tens, hundreds. The number 2026 means 2 thousands, 0 hundreds, 2 tens and 6 ones.
Other bases work exactly the same way, with a different multiplier. In binary (base 2) each position is worth twice the one to its right. In hexadecimal (base 16) each position is worth 16 times the one to its right.
Reading hexadecimal
Hex needs 16 digit symbols, so after 0 to 9 it uses A to F for 10 to 15. To read a hex number, multiply each digit by its place value and add them up.
7 in the 256s place
7 × 256equals1,792
E (14) in the 16s place
14 × 16equals224
A (10) in the ones place
10 × 1equals10
Total
1,792 + 224 + 10equals2,026
So 7EA in hex is 2026 in decimal. In binary it is 11111101010, and in octal (base 8) it is 3752.
Why hex is everywhere
Each hex digit stands for exactly four binary digits, so one byte (8 bits) is always exactly two hex digits. The largest byte value, 11111111 in binary, is FF in hex and 255 in decimal. That is why colour codes like #07804F, memory addresses and file signatures are written in hex: it is short, and it maps cleanly onto the underlying bits.
Quick reference
- Binary 1010 = hex A = decimal 10
- Binary 1111 = hex F = decimal 15
- Binary 10000 = hex 10 = decimal 16
- Binary 11111111 = hex FF = decimal 255
Octal is less common today, but you will still see it in Unix file permissions, where 755 means read, write and execute for the owner and read and execute for everyone else.
Binary to hex in one step
Because 16 is 2 to the power of 4, every hex digit stands for exactly four binary digits. To convert binary to hex, split it into groups of four from the right and convert each group. 11010110 becomes 1101 and 0110, which are D and 6, so the answer is D6. In decimal that is 13 × 16 + 6 = 214. Going the other way is just as quick, which is why programmers read memory and machine code in hex rather than long strings of ones and zeros.
Where you meet each base
- Hex: color codes (#FF8800), memory addresses, MAC addresses, IPv6 addresses, Unicode code points such as U+00E9, and hashes.
- Octal: Unix file permissions, where chmod 755 means rwx for the owner (7) and r-x for everyone else (5).
- Binary: bit flags, network masks, and anything that works with individual bits.
Writing numbers in code
Most languages use a prefix to show the base: 0x for hex (0xFF), 0b for binary (0b1010) and 0o for octal (0o755) in languages such as Python and JavaScript. Watch out for a leading zero on its own: in older C code and some other contexts, 0755 is read as octal, so 010 means 8, not 10.
Signed numbers
The same bits can mean different numbers depending on whether they are read as signed or unsigned. In 8 bits, 0xFF is 255 unsigned, but -1 in two's complement, the system almost all computers use for signed integers. If a value suddenly shows up as a large negative number, a signed/unsigned mix-up is a likely cause.
Converting decimal to binary by hand
Divide by 2 repeatedly and write down the remainders, then read them from bottom to top. For 13: 13 ÷ 2 = 6 remainder 1; 6 ÷ 2 = 3 remainder 0; 3 ÷ 2 = 1 remainder 1; 1 ÷ 2 = 0 remainder 1. Reading upwards gives 1101. The same method works for any base: divide by 16 for hex, by 8 for octal.
Going the other way, multiply each digit by its place value and add: 1101 is 8 + 4 + 0 + 1 = 13.
Powers of two worth remembering
- 2^8 = 256: the number of values in one byte (0 to 255).
- 2^10 = 1,024: why a kibibyte is 1,024 bytes.
- 2^16 = 65,536: the number of network ports.
- 2^32 = about 4.3 billion: the number of IPv4 addresses, and the range of a 32-bit integer.
Tip: To convert binary to hex by hand, split the binary number into groups of four from the right, and convert each group to a single hex digit.
Questions people ask
- How do I convert hexadecimal to decimal?
- Multiply each digit by its place value (1, 16, 256 and so on) and add them. 7EA is 7 × 256 + 14 × 16 + 10 = 2,026.
- Why do programmers use hexadecimal?
- Each hex digit is exactly four bits, so one byte is always two hex digits, which makes hex a compact way to write binary.
- Where is octal still used?
- Mainly in Unix file permissions, where 755 gives the owner read, write and execute, and everyone else read and execute.
- How do I convert binary to hexadecimal?
- Group the binary digits in fours from the right and convert each group. 11010110 splits into 1101 and 0110, which is D6 in hex, or 214 in decimal.
- How do I convert decimal to binary?
- Divide by 2 repeatedly, note each remainder, and read the remainders from bottom to top. 13 becomes 1101.