2.1.6 - Hexadecimal
Long strings of 0s and 1s are awkward to handle, but hexadecimal gives each group of four bits a single symbol. By the end of this lesson, you will be able to explain why people use this notation and convert accurately in both directions.
Hexadecimal digits and four-bit patterns
Hexadecimal
Hexadecimal is a base-16 number system that uses the 16 single-digit symbols 0 to 9 and A to F.
The letters are digits in this system: A represents ten, B represents eleven, through to F, which represents fifteen. For example, A is one hexadecimal digit; it is not two digits and it is not being used as an ordinary letter.
Four bits can make 16 different patterns because 2^4 = 16. That is an exact match for the 16 hexadecimal digits, so each hexadecimal digit corresponds to one four-bit pattern:
| Hexadecimal | Binary | Hexadecimal | Binary |
|---|---|---|---|
0 | 0000 | 8 | 1000 |
1 | 0001 | 9 | 1001 |
2 | 0010 | A | 1010 |
3 | 0011 | B | 1011 |
4 | 0100 | C | 1100 |
5 | 0101 | D | 1101 |
6 | 0110 | E | 1110 |
7 | 0111 | F | 1111 |
The important relationship is fixed: one hexadecimal digit matches exactly four binary bits. This lets you convert by matching groups rather than passing through denary.
Why hexadecimal is used
At hardware level, data is represented using binary bits. Hexadecimal is a notation that people and software displays use to show the same bit patterns more compactly.
Consider this binary pattern:
1110 1011 0100 1101
Replace each four-bit group with its matching hexadecimal digit:
| Binary group | 1110 | 1011 | 0100 | 1101 |
|---|---|---|---|---|
| Hexadecimal digit | E | B | 4 | D |
The same pattern can therefore be written as EB4D. The binary version has 16 bits to read and copy; the hexadecimal version has four digits. Because the relationship is one digit to four bits, a person can move directly between the short notation and the full bit pattern, making long patterns easier to read, write and compare.
Do not confuse a notation with the underlying representation: writing EB4D does not mean the hardware has stopped using binary. It is a compact human-facing way to display the same pattern.
Converting hexadecimal to binary
To convert from hexadecimal to binary:
- Read the hexadecimal digits from left to right.
- Replace each digit with its four-bit pattern from the table.
- Keep the groups in the same order and keep all four bits in every group.
Worked example: convert 3A7 to 12-bit binary.
| Hexadecimal digit | 3 | A | 7 |
|---|---|---|---|
| Four-bit pattern | 0011 | 1010 | 0111 |
So:
3A7 hexadecimal = 0011 1010 0111 binary
The spaces only show the four-bit groups; they are not extra digits.
Leading zeroes matter when a width is stated. For example:
0F hexadecimal = 0000 1111 binary
Writing only 1111 would not meet a request for an 8-bit pattern. The hexadecimal digit 0 still contributes its complete group 0000.
Converting binary to hexadecimal
To convert from binary to hexadecimal:
- Starting at the right, split the binary pattern into groups of four bits.
- If the leftmost group has fewer than four bits, add zeroes on its left until it has four.
- Replace each group with its matching hexadecimal digit, keeping the groups in order.
Worked example: convert 1101 0010 1111 to hexadecimal.
| Four-bit group | 1101 | 0010 | 1111 |
|---|---|---|---|
| Hexadecimal digit | D | 2 | F |
Therefore:
1101 0010 1111 binary = D2F hexadecimal
Starting from the right matters when the number of bits is not a multiple of four. For 101101, group from the right and pad only the short group on the left:
10 1101 -> 0010 1101 -> 2D
Do not add zeroes on the right. That would alter the final bit positions and produce a different pattern.
Checking and correcting conversions
A reliable check is to reverse your conversion. Expand every hexadecimal digit back into four bits and compare the result with the original binary pattern.
Suppose 0101 1110 1001 has been converted to 5E9. Reverse it:
| Hexadecimal digit | 5 | E | 9 |
|---|---|---|---|
| Four-bit pattern | 0101 | 1110 | 1001 |
The reconstructed pattern is exactly 0101 1110 1001, so the conversion is correct.
When checking, ask three precise questions:
- Is there one four-bit group for every hexadecimal digit?
- Are the groups still in their original left-to-right order?
- Does every group match the conversion table exactly, including leading zeroes?
This catches a common error such as writing the pattern for 7 as 111 instead of 0111. The shorter form may look similar, but it breaks the fixed one-digit-to-four-bits relationship and fails any stated bit width.