Hexadecimal
What hexadecimal is
Hexadecimal (hex) is base 16. It needs 16 digits, so after 0–9 it uses the letters A–F for 10–15:
| Denary | Binary | Hex |
|---|---|---|
| 0 | 0000 | 0 |
| 1 | 0001 | 1 |
| 2 | 0010 | 2 |
| 3 | 0011 | 3 |
| 4 | 0100 | 4 |
| 5 | 0101 | 5 |
| 6 | 0110 | 6 |
| 7 | 0111 | 7 |
| 8 | 1000 | 8 |
| 9 | 1001 | 9 |
| 10 | 1010 | A |
| 11 | 1011 | B |
| 12 | 1100 | C |
| 13 | 1101 | D |
| 14 | 1110 | E |
| 15 | 1111 | F |
One hex digit stands for exactly 4 bits (a nibble), so a byte is always two hex digits, from 00 (0) to FF (255).
Key terms
Why programmers use hex
Computers still store everything in binary: hex is just a way for people to write it.
- It's shorter: 11010110 is just D6.
- It's easier to read and remember, so there are fewer mistakes when reading or typing it.
- It converts to and from binary more easily than denary does, as each hex digit is one nibble.
You'll see hex in colour codes on web pages, such as #FF0000 for red, in MAC addresses of network cards, and in error codes.
Key terms
Hex and denary
In a two-digit hex number, the left digit is worth 16s and the right digit 1s.
Hex to denary: convert 3C.
- 3 × 16 = 48
- C is 12, so 12 × 1 = 12
- 48 + 12 = 60
Denary to hex: convert 200.
- Divide by 16: 200 ÷ 16 = 12 remainder 8
- The 16s digit is 12, which is C; the 1s digit is the remainder, 8
- So 200 is C8. Check: 12 × 16 + 8 = 200.
Key terms
Hex and binary, by nibbles
Binary to hex: split the byte into two nibbles, then convert each one using the place values 8, 4, 2, 1.
Example: convert 10101110.
| Left nibble | Right nibble | |
|---|---|---|
| Binary | 1010 | 1110 |
| Denary | 8 + 2 = 10 | 8 + 4 + 2 = 14 |
| Hex | A | E |
So 10101110 is AE.
Hex to binary: turn each hex digit into 4 bits, then join them. 5B: 5 is 0101 and B (11) is 1011, so 5B is 01011011.
You can also go between hex and denary through binary, if you find that easier.
Common mistake
Every hex digit becomes four bits, with leading zeros: 5 is 0101, not 101. Otherwise the bits shift into the wrong places.