Lesson 7 – Hexadecimal Numbers

Learning Objectives

  • Understand what hexadecimal numbers are and how to convert to and from binary.
  • Understand that whilst computers use binary, it is easier for humans to use hexadecimal to represent the numbers used by a computer

Learning Outcomes

All must try out HTML hex colour codes to see how hex is used in real life.  With help, complete both worksheets to turn binary numbers to hexadecimal and vice versa.  Have a go at one of the plenary games. (Level 4)

Most should try out HTML hex colour codes and have a basic understanding of how hex is used in real life.  Complete both worksheets to turn binary numbers to hexadecimal and vice versa.  Have a go at one of the plenary games. (Level 5)

Some could try out HTML hex colour codes and be able to explain how hex is used in real life.  Complete both worksheets to turn binary numbers to hexadecimal and vice versa.  Have a go at all of the plenary games. (Level 6)

Keywords

Words to learn: hexadecimal, binary, denary, convert

Starter

Click on this link.  On the left hand side is a simple HTML web page.  On the right hand side you can see the result.  Try changing the colours of the text to something different by using a different word – say green or purple:

Capture

That’s all well and good, but let’s say I want a pinky-purple, or a light greeny-blue.  I can’t use words to represent every possible colour visible to the human eye – there are 16777216 of them.  I counted!

So we can try something different, we can use codes to represent them.  Try out the following:

Capture

So we’re using codes that have 6 characters.  Where do these come from?  Well we can make up any colour by mixing red, green and blue.  The first two characters give how much red is in the colour, the next two characters how much green and the last two how much blue.  Try the following codes:

#000000
#FF0000
#00FF00
#0000FF
#FFFFFF

What colours do you get?  Can you explain it?  Try going to the following website which will give you the colour code for any colour (click here)

The codes you have been using are what is known as hexadecimal numbers (or base 16).  They are the sister number scheme to binary (or base 2).

For example the colour green is 00FF00 is hexadecimal.  In binary it is 000000001111111100000000 and in denary it is 65280 – as you can see, it’s much easier to write in hexadecimal!  We can also easily see that green is the colour as red and blue are switched off.

In hexadecimal,instead of there only being two values (0 and 1) as in binary, or ten values as in denary (0,1,2,3,4,5,6,7,8,9) there are sixteen different values (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F).  We use hexadecimal as it’s a lot shorter to write than binary and we can easily change hexadecimal numbers to binary and vice versa.

Watch this video which goes through why hexadecimal is such a useful number system:

Main – Binary to Hexadecimal

Turning a binary number into hexadecimal is quite easy.  Let’s use the number:

01101001

Step 1) Take the byte and split it into two nibbles (honestly, that’s the name for half a byte… get it… half a bite… who says Computing isn’t hilarious?)

That gives us 0110 and 1001

Step 2) Count from zero to fifteen in binary:

0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111

Step 3) Write hexadecimal digits next to the numbers you just wrote:

0000 = 0
0001 = 1
0010 = 2
0011 = 3
0100 = 4
0101 = 5
0110 = 6
0111 = 7
1000 = 8
1001 = 9
1010 = A
1011 = B
1100 = C
1101 = D
1110 = E
1111 = F

Step 4) Match each nibble up with the table.  So 0110 is 6 and 1001 is D.  So 01101001 in binary is 6D in hexadecimal.  Easy!

Have a go at this worksheet and try turning some binary numbers into hexadecimal numbers.

Main – Hexadecimal to Binary

If converting binary to hexadecimal is easy, then going from hexadecimal back to binary is just as easy.  Let’s use the hexadecimal number DC.

Step 1) Take the hexadecimal digit and split it into the two values – so we have D and we have C.

Step 2) Count from zero to fifteen in binary:

0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111

Step 3) Write hexadecimal digits next to the numbers you just wrote:

0000 = 0
0001 = 1
0010 = 2
0011 = 3
0100 = 4
0101 = 5
0110 = 6
0111 = 7
1000 = 8
1001 = 9
1010 = A
1011 = B
1100 = C
1101 = D
1110 = E
1111 = F

Step 4) Using the table, D is 1101 and C is 1100 – put the two together and you get 11011100.  So DC is 11011100 in binary.  Simple!

Have a go at this worksheet and try turning some hexadecimal numbers into binary numbers.

Plenary

So you think you’re some kind of hex master?  Try the games below to see how good you actually are:

Hex Invaders
What the Hex?
Flippy Bit and The Attack of the Hexadecimals From Base 16