Converting between number bases using hexadecimal
In computer science, different number bases are used:
- denaryThe number system most commonly used by people. It contains 10 unique digits 0 to 9. Also known as decimal or base 10. is base 10, which has ten units (0-9)
- binaryA number system that contains two symbols, 0 and 1. Also known as base 2. is base 2, which has two units (0-1)
hexadecimalA number system using 16 symbols from 0-9 and A-F, also known as base 16 and hex., also known as hex, is the third commonly used number system. It has 16 units - 0-9 and the letters A, B, C, D, E and F.
| Decimal | Binary | Hexadecimal |
| 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 |
| Decimal | 0 |
|---|---|
| Binary | 0000 |
| Hexadecimal | 0 |
| Decimal | 1 |
|---|---|
| Binary | 0001 |
| Hexadecimal | 1 |
| Decimal | 2 |
|---|---|
| Binary | 0010 |
| Hexadecimal | 2 |
| Decimal | 3 |
|---|---|
| Binary | 0011 |
| Hexadecimal | 3 |
| Decimal | 4 |
|---|---|
| Binary | 0100 |
| Hexadecimal | 4 |
| Decimal | 5 |
|---|---|
| Binary | 0101 |
| Hexadecimal | 5 |
| Decimal | 6 |
|---|---|
| Binary | 0110 |
| Hexadecimal | 6 |
| Decimal | 7 |
|---|---|
| Binary | 0111 |
| Hexadecimal | 7 |
| Decimal | 8 |
|---|---|
| Binary | 1000 |
| Hexadecimal | 8 |
| Decimal | 9 |
|---|---|
| Binary | 1001 |
| Hexadecimal | 9 |
| Decimal | 10 |
|---|---|
| Binary | 1010 |
| Hexadecimal | A |
| Decimal | 11 |
|---|---|
| Binary | 1011 |
| Hexadecimal | B |
| Decimal | 12 |
|---|---|
| Binary | 1100 |
| Hexadecimal | C |
| Decimal | 13 |
|---|---|
| Binary | 1101 |
| Hexadecimal | D |
| Decimal | 14 |
|---|---|
| Binary | 1110 |
| Hexadecimal | E |
| Decimal | 15 |
|---|---|
| Binary | 1111 |
| Hexadecimal | F |
Hex is useful because large numbers can be represented using fewer digitA single whole number value from 0 to 9, especially when used in a larger number. For example, the number 752 has 3 digits.. For example, colour values and MAC addressMedia access control - each unique piece of hardware on a network has a MAC address. are often represented in hex. Read more about MAC addresses in the network topologies, protocols and layers study guide.
Additionally, hex is easier for humans to understand than binary. Programmers often use hex to represent binary values as they are simpler to write and check than when using binary.
Hexadecimal to decimal
Whereas decimal place values are powers of 10, and binary place values are powers of 2, hex place values are powers of 16.
| 65,536 | 4,096 | 256 | 16 | 1 |
| 65,536 |
| 4,096 |
| 256 |
| 16 |
| 1 |
Each place value can be represented by the units 0 through to F.
To convert hex to decimal, simply take each place value that has a unit in it, and add them together.
Example - hex number 7C
| 65,536 | 4,096 | 256 | 16 | 1 |
| 7 | C |
| 65,536 | |
|---|---|
| 4,096 | |
| 256 | |
| 16 | 7 |
| 1 | C |
Result - (7 × 16) + (C × 1) = (7 × 16) + (12 × 1) = (112) + (12) = 124
Question
What would these hex numbers be in decimal?
- 11
- 2B
- FA
- 17
- 43
- 250
Converting between binary and hexadecimal numbers
Decimal to hexadecimal
The AQA specification requires you to be able to convert from decimal to numbers containing multiple digits in hexadecimal. To convert:
- If the decimal number is bigger than 16, divide it by 16. Take the hexadecimal equivalent of this result - this represents the first digit. Take the hexadecimal equivalent of the remainder - this represents the second digit.
- If the decimal number is smaller than 16, take the hexadecimal equivalent of the decimal number.
Example - convert decimal 22 to hexadecimal
16 goes into 22 once with 6 left over, so 22 ÷ 16 = 1 remainder 6
1 = hex 1
6 = hex 6
Result - 16
Example - convert 138 to hexadecimal
138 ÷ 16 = 8 remainder 10
8 = hex 8
10 = hex A
Result - 8A
Binary to hexadecimal
- Start at the rightmost digit and break the binary number up into groups of four digits. These are known as nibbleHalf a byte, 4 bits.. If there are less than four digits, use just that number of digits for that group.
- Next, convert each group of four digits into decimal.
- Convert each decimal value into its hex equivalent.
- Put the hex digits together.
Example - 1101 to hex
1101 = decimal 13
13 = hex D
Result - D
Example - 11000011 to hex
Break into groups of four - 1100 0011
1100 = decimal 12 0011 = decimal 3
12 = hex C 3 = hex 3
Result - C3
Example - 110011 to hex
Break into groups of four - 0011 0011. In this example, extra 0s are added at the highest values to create two groups of four bits.
0011 = decimal 3 0011 = decimal 3
3 = hex 3 3 = hex 3
Result - 33
Hexadecimal to binary
- Split the hex number into individual values.
- Convert each hex value into its decimal equivalent.
- Next, convert each decimal digit into binary, making sure to write four digits for each value.
- Combine all four digits to make one binary number.
Example - hex 28 to binary
2 = decimal 2 8 = decimal 8
2 = binary 0010 8 = binary 1000
Result - 00101000
Example - hex FC to binary
F = decimal 15 C = decimal 12
15 = binary 1111 12 = binary 1100
Result - 11111100
Question
What would these hex numbers be in binary?
- 11
- 2B
- AA
- 00010001
- 00101011
- 10101010