Beyond the Decimal and Binary numbering systems, Hexadecimal is the next most commonly used number base in computer science. In today’s post we take a look at this multi-purpose number base, examining the similarities between it and the aforementioned number bases of binary and decimal before learning some tricks in how to convert numbers to and from hexadecimal notation.

## What is Hexadecimal Notation?

In some of my previous posts we learnt about how we can write different numbers using different notations. We learnt about Decimal notation and how it used a number base (or radix) of 10 and the characters 0 – 9 to write its numbers. We also learnt about a second notation, Binary and how instead of using a radix of 10, it uses a radix of 2 and the characters 0 and 1 to express numbers in this notation. We also saw how, although these notations are different, they were both based on the common concept where numbers in the respective notations were expressed in terms of powers of the radix or number base.

In Computer Science, Hexadecimal (also known as Hex) is another such notation. Where Hexadecimal differs from Decimal and Binary is its number base. Hexadecimal is based on a radix or number-base of 16. Numbers in hexadecimal use the characters 0 – 9 and A-F (or a – f) and are sometimes written with the base as a subscript to distinguish them from numbers written in Binary or Decimal. For example:

Decimal Number | Hexadecimal Equivalent |

0 | 0 |

1 | 1 |

2 | 2 |

3 | 3 |

4 | 4 |

5 | 5 |

6 | 6 |

7 | 7 |

8 | 8 |

9 | 9 |

10 | A |

11 | B |

12 | C |

13 | D |

14 | E |

15 | F |

The most common use for Hexadecimal is to express colours and you may have seen this before, especially if you have ever written your own web page or used any graphics applications. When Hexadecimal is used in this way, 24-bits of colour information (representing the relative proportions of red, green and blue that make up the colour) are written as three pairs of hexadecimal digits in the form #RRGGBB where RR represents the red component, GG represents the green component and BB represents the blue component. A few examples included, the number hex number #DFFFF8 which represents a blue / green color and the hex number #7334A4 which represents a purple color. In these examples the # symbol (the hash our pound sign) is used to indicate the number is a hexadecimal number.

The main benefit of using Hexadecimal notation is that it allows us to write out the value of a byte in a human readable format using only two characters. Those two characters are still able to represent all 256 possible values of the byte.

Underlying all this, the same rules that we learnt when looking at Binary and Decimal still apply. At their most basic, numbers, when expressed in Hex, are expressed in terms of powers of 16. The different number columns are:

Power | 16 | 16 | 16 | 16 | 16 | 16 | 16 |

Decimal Equivalent | 16777216 | 1048576 | 65536 | 4096 | 256 | 16 | 1 |

In the next few sections we’ll look at how we use these number columns and convert numbers between the different number bases.

## Converting from Decimal to Hexadecimal

The process to convert from a decimal number into a hexadecimal number is an iterative one.

We’ve already learnt in one of my previous posts how to perform division by hand (you really should go back and read it if you are still unclear) so I’m not going to go through all the division steps in detail. Instead, I’m just going to describe those steps that are relevant for converting from decimal to hexadecimal.

Examples are always best so to illustrate the steps involved we’re going to convert the decimal number 43894 into hexadecimal notation.

Step 1 of the process is to perform integer division and divide our decimal number by 16. This results in a quotient (above the line) and a remainder at the bottom. In this case the quotient is 2743 and the remainder is 6. | |

6 | Step 2 is to convert the remainder and express it in hexadecimal notation. As 6 in decimal is also expressed as 6 in hexadecimal we have our first digit. |

In the next step, we take the quotient we calculated in step 1 (2743) and work out whether it is less than 16. As it isn’t, we go back and repeat steps 1 and 2 above dividing the quotient from step 1 by 16 to calculate a new quotient and new remainder. | |

In our example we divide 2743 by 16 to get a new quotient of 171 and a new remainder of 7. | |

7 | Again we convert the remainder from decimal to hexadecimal notation. As 7 in decimal is also 7 in hexadecimal we have our second digit. |

Next, we check our new quotient and see whether it is less than 16. Again, it isn’t so we repeat steps 1 and 2, this time with our quotient of 171. | |

This time, if we divide 171 by 16 we obtain a new quotient of 10 and a new remainder of 11. | |

11 | We convert the remainder of 11 in decimal into the equivalent in hexadecimal. |

10 | Again we compare our quotient to 16. This time the quotient is less than 16 so instead of repeating steps 1 and 2 we simply take the quotient and express it in hexadecimal notation. |

43894 | The final step is to read off our last quotient (in hexadecimal) and then all our remainders (in hexadecimal) in reverse order. If we concatenate all these, we have our original number in hexadecimal notation. In this case we get the number AB76 |

## Converting from Hexadecimal into Decimal

The process from converting from Hexadecimal into Decimal is far easier than the reverse. As we’ve learnt previously with both binary and decimal notation, instead of expressing a hexadecimal number as just the multipliers, we can also write it in its expanded form. For example:

89AB_{16} = (8 x 16^{3}) + (9 x 16^{2}) + (A x 16^{1}) + (B x 16^{0})

If we then convert each of the individual multipliers into binary we get:

89AB_{16} = (8 x 16^{3}) + (9 x 16^{2}) + (10 x 16^{1}) + (11 x 16^{0})

**89AB _{16}** = (8 x 4096) + (9 x 256) + (10 x 16) + (11 x 1) = 32768 + 2304 + 160 + 11 =

**35243**

_{10}## Converting from Binary to Hexadecimal

Converting from a binary number into its hexadecimal equivalent is also a relatively easy process as well. The first step is to divide the binary number into groups of four digits and add any leading zeros that are necessary to make these up to four digit groups.

If we use the example of 101101011_{2} we would get the following (we would need to prepend three leading zeros):

0001 0110 1011

The next step in the conversion process is to take each four digit group in isolation and calculate its decimal equivalent.

From there, then calculate the equivalent hexadecimal representation. I’ve extended our previous table to help you with this conversion. Once you become more familiar with binary and hexadecimal numbers, you can skip the intermediate conversion to decimal and perform the conversion directly from binary into hexadecimal.

4-Digit Binary Number | Decimal Equivalent | Hexadecimal Equivalent |

0000 | 0 | 0 |

0001 | 1 | 1 |

0010 | 2 | 2 |

0011 | 3 | 3 |

0100 | 4 | 4 |

0101 | 5 | 5 |

0110 | 6 | 6 |

0111 | 7 | 7 |

1000 | 8 | 8 |

1001 | 9 | 9 |

1010 | 10 | A |

1011 | 11 | B |

1100 | 12 | C |

1101 | 13 | D |

1110 | 14 | E |

1111 | 15 | F |

Once you have performed the conversion for each of the individual groups of bits, you can then join all the calculated hexadecimal digits to form the final number. If we do this with our example above we get:

0001_{2} = 1_{10} = 1_{16}

0110_{2} = 6_{10} = 6_{16}

1011_{2} = 11_{10} = B_{16}

And then when we join them we get:

101101011_{2} = 16B_{16}

## Converting from Hexadecimal to Binary

The final thing we’re going to look at today is how to convert from hexadecimal into binary.

There are two main approaches that you can take for this:

Convert the whole number into to Decimal First and then into Binary

Convert Directly from Hexadecimal into Binary

The first approach to converting from hexadecimal into binary is to first convert the hexadecimal number into decimal notation and then perform a second conversion from decimal notation into binary. We’ve already seen earlier in this post how to perform the first of these conversions and in a previous post we’ve seen how to perform the second. Although this approach works, personally I find it rather long-winded and actually prefer the second approach, that of converting into binary directly.

If you’re going to convert a hexadecimal number directly into its binary equivalent, we basically perform the steps from the previous section in reverse. Let’s take the hexadecimal number 5AF2_{16} as an example.

We already know that each hexadecimal character represents four binary bits so we’ll take each of the hexadecimal characters in turn. Starting at the most significant character we have:

5_{16} = 0101_{2}

A_{16} = 1010_{2}

F_{16} = 1111_{2}

2_{16} = 0010_{2}

If we now take each of the binary results and concatenate them we have the result:

5AF2_{16}= 101 1010 1111 0010

_{2}