Module Progress
0% Complete

So far we have looked at two number systems – base 2 (binary) and base 10 (decimal). Another common number system using in Computer Science is base 16, otherwise known as hexadecimal, or hex for short.

Why do we use hex? Well, it’s a multiple of 2, so it is relatively easy to move between binary and hex, and can store these values in less space, as an 8 digit binary value would take just 2 hex digits.

Because hex is good at representing very large binary values it is used in many areas of computing, like character encoding, memory addressing, RGB colour values and IPv6 addresses.

In this lesson, we’ll learn about:

2. How to convert numbers from decimal to hexadecimal.
3. How to convert numbers from hexadecimal to decimal.
4. How to convert numbers from binary to hexadecimal.
5. How to convert numbers from hexadecimal to binary.
Media Attachments:

## 1. What is Hexadecimal Notation?

Hexadecimal notation is a numbering system where everything goes up in multiples of 16.

However, because our decimal numbering system only goes from 0-9, to get the remaining numbers we have to use letters A-E, as shown in the table below.

As with binary, the way to get this right is to write down your Hex table before you start.

So in hexadecimal, the number 6 is 6 just like decimal, but the number 14 is E.

### Further Thought

In the lesson introduction we saw some of the ways hex is used in computing. Research more about these and why hex is so important.

## 2. How to Convert Decimal to Hex

To find out the hex value of a decimal number we follow a similar process to working out the binary.

The difference between binary and hex conversions is that now we have to work out how many TIMES the column number will fit into our desired number.

The difference between binary and hex is that with binary, each number to the left increases in multiples of 2. With hexadecimal it increases by multiples of 16. So our columns are as follows:

### Worked Example 1

Let’s work out the hex value for the decimal number 20.

Just like converting to binary, we ask similar questions and follow a similar process:

Will 4096 fit into 20? No, so we don’t have any of these, so we put a 0 in the 4096 column.

Will 256 fit into 20? No, so we don’t have any of these, so we put a 0 in the 256 column.

Will 16 fit into 20? Yes. How many times? 1 – so we put 1 in the 16 column.

Now we have to take 16 away from 20. 20 – 16 = 4.

Will 1 fit into 4? Yes. How many times? 4 – so we put 4 in the 1 column.

So, the hex value for 20 is 14 (and you say it as ONE – FOUR). That’s a bit confusing – so let’s try another example with a larger number.

### Worked Example 2

Let’s work out the hex value for the decimal number 109.

Will 4096 fit into 109? No, so we don’t have any of these, so we put a 0 in the 4096 column.

Will 256 fit into 109? No, so we don’t have any of these, so we put a 0 in the 256 column.

Will 16 fit into 20? Yes. How many times? 6 – so we put 6 in the 16 column.

6 x 16 is 96, so we have to take 96 away from 109. 109 – 96 = 13.

Will 1 fit into 13? Yes. How many times? 13 of course, but what is the hex code for 13? Using the table near the start of this lesson we can see that it is D.

So we put D into the 1 column.

So, the hex value for 109 is 6D.

### Worked Example 3

Let’s try one last number. A really big one this time. Let’s try 5078.

Will 4096 fit into 5078? Yes. How many times? 1 – so we put 1 in the 4096 column.

Now we have to take 4096 away from 5078. 4096 – 5078 = 982.

Will 256 fit into 982? Yes. How many times? 3 – so we put 3 in the 256 column.

3 x 256 is 768, so we have to take 768 away from 982. 982 – 768 = 214.

Will 16 fit into 214? Yes. How many times? 13 – but what is the hex code for 13? Using the table near the start of this lesson we can see that it is D.

So we put D in the 16 column.

13 x 16 is 208, so we have to take 208 away from 214. 214 – 208 = 6.

Will 1 fit into 6? Yes. How many times? 6 – so we put 6 in the 1 column.

So, the hex value for 5078 is 13D6.

### Further Thought

What do you think would come after the 4096 column? See if you can work it out and check your answer online.

## 3. How to Convert Hex to Decimal

Converting from hexadecimal to decimal is straightforward. All we need is our hex table from the beginning of this lesson.

Let’s look at the example below:

We have nothing in the 4096 and the 256 columns, so we can leave those alone.

We have D in the 16 column. We can see from our hex table that D is the hex value for 13. So we need to multiple 13 x 16, which gives us 208.

In the 1’s column we have 9, which we don’t need to do anything to.

Now we just need to add these numbers together. 208 + 9 = 217.

So, the decimal value for D9 is 217.

### Further Thought

Mastering hex to decimal conversion is simple, but does take some practice. It helps if you learn the 0-F number by heart. Practice this with a friend.

## 4. How to Convert Binary to Hex

In a previous lesson we learned that a nibble was 4 bits. The smallest number we can store in 4 bits is 0.

The largest number we can store in 4 bits is 15.

In hex each column goes from 0 – 15, therefore each column can be represented as a set of nibbles.

Here’s how we do this:

### Worked Example 1

Let’s take the binary number 100011.

What we need to do is turn these 6 bits into two 4 bit nibbles. This means we’ll need to add two 0’s to the left side of this number. This gives us:

Now, let’s split this into two seperate nibbles and treat them as two seperate binary numbers.

We can then convert these two number easily into hexadecimal by adding up the column values that have 1’s in them, just like we do in binary to decimal conversion.

The first number is equal to 2.

The second number is equal to 3.

So, the binary number 100011 in hexadecimal is 23. Note, that’s TWO-THREE, not twenty-three.

### Worked Example 2

Consider the number 11101010.

This is already 8 bits, so we can straightaway split it into our two nibbles.

We now can convert these two nibbles into their hexadecimal values.

The first number is equal to 14, which in hexadecimal is E.

The second number is equal to 10, which in hexadecimal is A.

So, the binary number 11101010 in hexadecimal is EA.

### Further Thought

What would you do if you had a 9 digit binary number? If you aren’t sure, research the answer online.

## 5. How to Convert Hex to Binary

Yet again we need to understand that each hex column is basically a binary nibble – a number that goes from 0 – 15.

Let’s say you have a hex number, we’ll use 4D.

What we need to do is recognise that these are two individual nibbles, and so we work out these separately.

First, we convert the number 4 into binary.

Then we convert D, which is the 13 in decimal, into binary.

The we simply stick them together.

### Further Thought

What is the largest binary number you can represent with two hex digits? The answer is simple, but do you know it?

## Lesson Summary

So to summarise what we’ve learnt in this lesson:

• Hex notation involves columns that are multiples of 16.
• Hex is used to make long binary sequences easier to read.
• Hex is used in IPv6 and in Assembler programming.
• The numbers 10 – 15 are represented by the letters A – F in hex.
• To convert from decimal to hex we need to divide rather than subtract.
• To convert from hex to decimal, we multiply the number in each column by the column value.
• Converting to and from binary involved treating each column as a nibble.
Quizzes