Hexadecimal (hex) is base 16: it has sixteen digits, 0 to 9 and then A to F. Because 16 = 24, one hex digit is exactly four bits, so hex is a shorter way to write binary, not a different way for a computer to store it.
This lesson builds on converting between binary and denary and sits inside Representing numbers and text.
What is the map from four bits to one hex digit?
Four bits (a nibble) can hold 0 to 15, which is exactly the sixteen hex digits.
| Denary | Binary | Hex | Denary | Binary | Hex |
|---|---|---|---|---|---|
| 0 | 0000 | 0 | 8 | 1000 | 8 |
| 1 | 0001 | 1 | 9 | 1001 | 9 |
| 2 | 0010 | 2 | 10 | 1010 | A |
| 3 | 0011 | 3 | 11 | 1011 | B |
| 4 | 0100 | 4 | 12 | 1100 | C |
| 5 | 0101 | 5 | 13 | 1101 | D |
| 6 | 0110 | 6 | 14 | 1110 | E |
| 7 | 0111 | 7 | 15 | 1111 | F |
You do not need to memorise the table. The binary column is just the 8, 4, 2, 1 place values, so you can rebuild any row in seconds.
How do you convert binary to hex and back?
Binary to hex:
- Split the bits into groups of four, starting from the right. Pad the left with zeros if the last group is short.
- Convert each group to a number from 0 to 15 using 8, 4, 2, 1.
- Write each number as a hex digit.
Hex to binary: replace each hex digit with its four bits.
Hex to denary: multiply the left digit by 16 and add the right digit.
Worked example
Convert 10111100 to hex, then to denary.
Step 1, group: 1011 and 1100.
Step 2, convert each group: 1011 = 8 + 2 + 1 = 11 = B. And 1100 = 8 + 4 = 12 = C.
Step 3, hex answer: BC.
Step 4, denary: B is 11 and C is 12, so 11 × 16 + 12 = 176 + 12 = 188.
Check: 10111100 in binary is 128 + 32 + 16 + 8 + 4 = 188. Both routes agree.
Going the other way, hex 3F becomes 0011 and 1111, so the binary is 00111111. In denary, 3 × 16 + 15 = 63, and 32 + 16 + 8 + 4 + 2 + 1 = 63 as well.
Can you check hex conversions in code?
Yes. In Python, hex(188) gives '0xbc', int("BC", 16) gives 188 and format(188, "08b") gives 10111100. Try them in the Python reasoning sandbox.
Use code to confirm your paper working, not to replace it, because the exam asks you to show the method.
The mistake to watch for
A common slip is grouping the bits from the left when the length is not a multiple of four.
Question: Convert 1101101 (7 bits) to hex.
Mistaken working: group from the left: 1101 and 101, so D and 5, giving D5.
D5 is 213 in denary, but 1101101 is 64 + 32 + 8 + 4 + 1 = 109. Group from the right and pad the left: 0110 1101, which is 6 and 13, so 6D. Check: 6 × 16 + 13 = 109.
Check yourself
1. Convert hex 7E to binary and denary.
Show answer
7 = 0111 and E = 14 = 1110, so the binary is 01111110. Denary: 7 × 16 + 14 = 112 + 14 = 126. Check: 64 + 32 + 16 + 8 + 4 + 2 = 126.
2. Convert 11110001 to hex.
Show answer
Groups: 1111 and 0001. 1111 = 15 = F and 0001 = 1. The answer is F1. In denary that is 15 × 16 + 1 = 241.
3. Give one reason a programmer might write 255 as FF.
Show answer
FF stands for 1111 1111, the largest 8-bit value (255). It takes two characters instead of eight, so it is shorter and easier to read and to copy without a mistake.
Where this leads next
Next, practise adding binary numbers and noticing overflow, then test the module with the mixed practice set. Hex gets quick once the four-bit groups feel automatic, and a teacher can drill that with you in online one-to-one Computer Science tuition.