Fundamentals of data representation - AQAConverting between number bases using hexadecimal

All data is represented as binary digits, whether it is numbers, text, images or sound. Calculations are also made in binary.

Part ofComputer ScienceComputational thinking and problem solving

Converting between number bases using hexadecimal

In computer science, different number bases are used:

  • is base 10, which has ten units (0-9)
  • is base 2, which has two units (0-1)

, 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.

DecimalBinaryHexadecimal
000000
100011
200102
300113
401004
501015
601106
701117
810008
910019
101010A
111011B
121100C
131101D
141110E
151111F
Decimal0
Binary0000
Hexadecimal0
Decimal1
Binary0001
Hexadecimal1
Decimal2
Binary0010
Hexadecimal2
Decimal3
Binary0011
Hexadecimal3
Decimal4
Binary0100
Hexadecimal4
Decimal5
Binary0101
Hexadecimal5
Decimal6
Binary0110
Hexadecimal6
Decimal7
Binary0111
Hexadecimal7
Decimal8
Binary1000
Hexadecimal8
Decimal9
Binary1001
Hexadecimal9
Decimal10
Binary1010
HexadecimalA
Decimal11
Binary1011
HexadecimalB
Decimal12
Binary1100
HexadecimalC
Decimal13
Binary1101
HexadecimalD
Decimal14
Binary1110
HexadecimalE
Decimal15
Binary1111
HexadecimalF

Hex is useful because large numbers can be represented using fewer . For example, colour values and 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,5364,096256161
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,5364,096256161
7C
65,536
4,096
256
167
1C

Result - (7 × 16) + (C × 1) = (7 × 16) + (12 × 1) = (112) + (12) = 124

Question

What would these hex numbers be in decimal?

  • 11
  • 2B
  • FA

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

  1. Start at the rightmost digit and break the binary number up into groups of four digits. These are known as . If there are less than four digits, use just that number of digits for that group.
  2. Next, convert each group of four digits into decimal.
  3. Convert each decimal value into its hex equivalent.
  4. 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

  1. Split the hex number into individual values.
  2. Convert each hex value into its decimal equivalent.
  3. Next, convert each decimal digit into binary, making sure to write four digits for each value.
  4. 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