On this page
Computers store everything as long strings of 0s and 1s, and people are bad at reading them. Hexadecimal, or hex, is the fix programmers settled on: it writes the same bits four at a time, so a 32-character binary string shrinks to eight symbols you can read aloud.
What is hexadecimal?
Hexadecimal is base 16. It uses sixteen digits: 0 to 9, then A, B, C, D, E and F for ten to fifteen. In decimal each place is worth ten times the one to its right; in hex each place is worth sixteen times, so 2F means 2 × 16 + 15 = 47.
The reason programmers use it is one line of arithmetic: 16 = 2 × 2 × 2 × 2. Four binary digits can make exactly sixteen patterns, from 0000 to 1111, so every hex digit matches one group of four bits and nothing is left over. Decimal has no such fit, because 10 is not a power of 2, which is why turning decimal into binary means repeated division by 2, as in our guide to converting decimal to binary and adding binary by hand.
How do you convert binary to hexadecimal?
You never divide. You group.
Start from the right and split the bits into groups of four.
If the leftmost group is short, pad it with 0s on the left.
Read each group with the place values 8, 4, 2, 1 and write the matching hex digit.
Worked example: 11010110.
Split: 1101 0110.
1101 is 8 + 4 + 1 = 13, which is D.
0110 is 4 + 2 = 6.
So 11010110 in binary is D6 in hex. As a check, D6 is 13 × 16 + 6 = 214 in decimal.
A second one with an awkward length: 101101 has six bits. Pad to 0010 1101, which gives 2 and D, so the answer is 2D, or 45 in decimal.
How do you convert hexadecimal back to binary?
Run the same table backwards: replace each hex digit with its four bits and keep the leading zeros inside each group. 3F becomes 0011 1111. A7 becomes 1010 0111.
Programming languages mark hex with a prefix so it is not mistaken for decimal. In Python, the built-in hex() function turns an integer into a lowercase hex string starting with "0x": hex(255) gives '0xff'. The same page notes that int() converts a hex string back using base 16, so you can check your hand conversions in two lines.
What is hexadecimal used for in colour codes?
The colour codes in web pages and design apps are hex. The W3C CSS Color Module Level 4 defines a hex colour as "#" followed by 3, 4, 6 or 8 hex digits. In the six-digit form, the first pair is red, the second green and the third blue, with 00 as the minimum and ff (255 in decimal) as the maximum. The specification gives #00ff00 as the same colour as rgb(0 255 0), a lime green.
Try it on #FFD166, the yellow in the diagram at the top of this post:
FF is 1111 1111, red 255 (full).
D1 is 1101 0001, green 13 × 16 + 1 = 209.
66 is 0110 0110, blue 6 × 16 + 6 = 102.
Why are memory addresses written in hexadecimal?
A memory address is a number that tells the computer which byte to fetch, and those numbers are long. In CPython, the id() function returns the address of the object in memory, so hex(id(x)) shows you where a value lives. A 64-bit number needs 64 binary digits but only 16 hex digits, and each digit still maps onto four bits.
Network addresses follow the same habit. RFC 4291, the IPv6 addressing standard, says the preferred text form is eight 16-bit pieces separated by colons, each written as one to four hex digits, as in 2001:DB8:0:0:8:800:200C:417A. Text encoding does it too: the code points behind Hindi letters are written in hex, which our post on how computers store Hindi text with Unicode and UTF-8 walks through byte by byte.
A 10-minute plan to make it stick
Write the sixteen four-bit patterns, 0000 to 1111, with their hex digits beside them, from memory.
Convert three binary numbers of odd lengths to hex, padding on the left.
Convert three hex numbers back to binary, keeping every leading zero.
Decode one colour code you see today into its red, green and blue values.
Check everything with hex() and int(x, 16) in Python.
If base conversion still feels shaky, the Fermi Concept Library covers every concept in mathematics and computing, each with its own lesson. When a particular conversion keeps coming out wrong, the Fermi Problem Library has a step-by-step solver that explains the reasoning rather than printing an answer, so you can see which group of four went astray. And if you are starting C this year, printing values in hex while you trace small first-semester C programs is a quick way to make the habit automatic.



