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 |
010 | 016 |
110 | 116 |
210 | 216 |
310 | 316 |
410 | 416 |
510 | 516 |
610 | 616 |
710 | 716 |
810 | 816 |
910 | 916 |
1010 | A16 |
1110 | B16 |
1210 | C16 |
1310 | D16 |
1410 | E16 |
1510 | F16 |
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 | 166 | 165 | 164 | 163 | 162 | 161 | 160 |
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. | |
610 = 616 |
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. | |
710 = 716 |
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. | |
1110 = B16 |
We convert the remainder of 11 in decimal into the equivalent in hexadecimal. |
1010 = A16 |
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. |
4389410 = AB7616 |
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 AB7616. |
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:
89AB16 = (8 x 163) + (9 x 162) + (A x 161) + (B x 160)
If we then convert each of the individual multipliers into binary we get:
89AB16 = (8 x 163) + (9 x 162) + (10 x 161) + (11 x 160)
If we now multiply everything out and sum the result:
89AB16 = (8 x 4096) + (9 x 256) + (10 x 16) + (11 x 1) = 32768 + 2304 + 160 + 11 = 3524310
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 1011010112 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 |
00002 | 010 | 016 |
00012 | 110 | 116 |
00102 | 210 | 216 |
00112 | 310 | 316 |
01002 | 410 | 416 |
01012 | 510 | 516 |
01102 | 610 | 616 |
01112 | 710 | 716 |
10002 | 810 | 816 |
10012 | 910 | 916 |
10102 | 1010 | A16 |
10112 | 1110 | B16 |
11002 | 1210 | C16 |
11012 | 1310 | D16 |
11102 | 1410 | E16 |
11112 | 1510 | F16 |
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:
00012 = 110 = 116
01102 = 610 = 616
10112 = 1110 = B16
And then when we join them we get:
1011010112 = 16B16
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 5AF216 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:
516 = 01012
A16 = 10102
F16 = 11112
216 = 00102
If we now take each of the binary results and concatenate them we have the result:
5AF216 = 101 1010 1111 00102
Summary
I’m going to leave it there for today. In this post we’ve learnt how hexadecimal notation is another of the key number notations used in computer science. We’ve seen how it differs from binary and decimal by using a radix or number base of 16 and how numbers expressed in hexadecimal notation are represented using the characters 0 – 9 and A – F. We’ve also then seen how to convert numbers to and from hexadecimal and both binary and decimal notation. As ever, if you have any questions or want to add anything, please leave a message in the comments below.