Without even realizing it, you use number conversion all the time. After all, knowing a 5K race means you have to run 3.1 miles is converting from the metric system to US measurement.
Or, if you’re watching an old movie and try to figure out when it was made from the Roman numerals in the copyright statement in the credits, that’s number conversion too.
Unless you’re a programmer, though, you’re probably not familiar with hexadecimal – that is, base 16 – numbering. But, if you do write code, you also know about the binary base-2 system. And, upon occasion, you’re going to need to convert hexadecimal numbers to binary.
So, what exactly are the hexadecimal and binary numbering systems? When are you going to use them? And how do you convert a hexadecimal number to binary? We’ll go over all the pertinent information you need to know below.
The Binary Number System
The numbering system everyone is most familiar with is base 10, also known as decimal or denary. That’s when each number position can have a value from zero to nine.
Most people also recognize the base-2 binary system where every number is represented by a combination of zeros and ones. For example, while the number 1 is represented as 1 in binary, the number 2 is expressed as 10.
Given the overlap in terms of how numbers can be represented, though, it can be confusing to determine which system is being used. To make the distinction clear, a subscript denoting the system can be used after the number: 1010, 10decimal, or 10d for decimal and 102, 10binary, or 10b for binary.
Binary numbers are used primarily with computers because, at the most basic level, computers only recognize two states: on or off. Each zero or one is a “bit” which is short for “binary digit.” A string of four bits is a nibble, and eight bits are a byte. A byte is also the number of bits needed to represent a character.
For the sake of comparison, here are the numbers 1-10 in both the decimal and binary number systems.
Decimal |
Binary |
---|---|
1 |
1 |
2 |
10 |
3 |
11 |
4 |
100 |
5 |
1010 |
6 |
110 |
7 |
111 |
8 |
1000 |
9 |
1001 |
10 |
1010 |
The Hexadecimal Number System
The hexadecimal system, however, is base 16 as there are sixteen possible values for each digit position. So, you may be asking, how can you have a base-16 system when we only have numerals from zero to nine?
Hexadecimal – or hex, as it’s commonly known – does this by using a combination of numbers and letters: 0-9 plus A-F for values from ten to fifteen. Therefore, while the number 1 in both decimal and hex is 1, the number 10 in decimal would be A in hex, and 25 in decimal would be 19 in hex. To make it clear you’re using hex, a subscript can be used after the number: 1016, 10hex, or 10h.
Again, for the sake of comparison, here are the numbers 11-20 in decimal, hex, and binary.
Decimal |
Hex |
Binary |
---|---|---|
11 |
B |
1011 |
12 |
C |
1100 |
13 |
D |
1101 |
14 |
E |
1110 |
15 |
F |
1111 |
16 |
10 |
10000 |
17 |
11 |
10001 |
18 |
12 |
10010 |
19 |
13 |
10011 |
20 |
14 |
10100 |
Hex is particularly useful in computing because of the amount of data compression it allows versus binary numbers because, by default, each hex digit is equivalent to one byte (four binary digits). Given that, while it takes four digits to express 11112 as a binary number, you only need one (F16) to do the same in hex.
As numbers increase in size, this becomes even more helpful. For example, it takes four digits to express 567810 and its hex equivalent 162E16, but this will take thirteen digits in binary: 10110001011102. In addition, converting from hex to binary and vice versa is much quicker – especially when a computer is doing it hundreds or thousands of times per second – than performing the same process with decimal numbers.
Hex is also used in the following computing situations:
- Displaying error messages: Because memory addresses use hex, this makes resolving error messages easier as they reference these memory locations.
- Defining colors on web pages: Each primary color on a web page – red, blue, and green – is represented by two hex numbers which, when combined to create other colors, use six hex numbers to represent the amount of each primary color being used.
- Representing Media Access Control (MAC) addresses: With these twelve-digit hex numbers, the first six identify the adapter manufacturer while the second six are the adapter’s serial number.
Converting from Hex to Binary
Converting from hex to binary is relatively simple – more so than converting either of these to or from a decimal number – as each hex digit is equivalent to four binary digits as per this table:
hex |
Binary |
---|---|
0 |
0000 |
1 |
0001 |
2 |
0010 |
3 |
0011 |
4 |
0100 |
5 |
0101 |
6 |
0110 |
7 |
0111 |
8 |
1000 |
9 |
1001 |
A |
1010 |
B |
1011 |
C |
1100 |
D |
1101 |
E |
1110 |
F |
1111 |
By referring to the table above, converting a hex number to binary only requires replacing each individual hex digit with its binary counterpart. For example, let’s convert 162E16 into a binary number using the four steps below.
1. Separate the hex digits
162E16 breaks out into four separate digits: 1, 6, 2, and E.
2. Replace the hex digits with binary equivalent digits
As per the table above, these are the binary representations for each of these hex digits:
hex |
Binary |
---|---|
1 |
0001 |
6 |
0110 |
2 |
0010 |
E |
1110 |
3. Combine the binary digits into a single string
0001 0110 0010 1110 becomes 0001011000101110.
4. Delete any zeros at the beginning of the number
Finally, you need to delete any zeros before the first one in the string. Therefore, 0001011000101110 becomes 10110001011102 after deleting the initial three zeros.
Let’s do two more examples to see how this works in action.
Example 1: Convert 62F7 to Binary
- 62F716 breaks out into the individual hex digits 6, 2, F, 7.
- 6, 2, F, 7 converts to the binary numbers 0110, 0010, 1111, 0111.
- 0110, 0010, 1111, 0111 becomes the single string 0110001011110111.
- 0110001011110111 becomes 1100010111101112 after deleting the zero beginning the string.
Example 2: Convert A24B7 to Binary
- A24B716 breaks out into the individual hex digits A, 2, 4, B, 7.
- A, 2, 4, B, 7 converts to the binary numbers 1010, 0010, 0100, 1011, 0111.
- 1010, 0010, 0100, 1011, 0111 becomes the single string 10100010010010110111.
- 101000100100101101112 is the final form of this binary number as there are no zeros before the first one which need to be deleted.
Converting from Binary to Hex
On occasion, you’ll need to convert a binary number to hex. This is also relatively simple except for also needing to account for any zeros at the beginning of the binary number which are not present. For example, let’s take another look at 62F716 from above which is expressed in 1100010111101112 in binary form.
1. Working from right to left, break out the binary number into groups of four digits
1100010111101112 becomes 110, 0010, 1111, 0111. You must work from right to left to account for any deleted zeros which began the binary number. With this binary number, for example, after moving from right to left, the first set of numbers only has three digits: 110. This indicates you need to put a zero at the beginning of it.
2. Convert individual binary 4-digit groups to hex
0110, 0010, 1111, 0111 converts to 6, 2, F, 7.
3. Combine the hex digits into a single string
6, 2, F, 7 becomes 62F716.
Online Hex to Binary Converters
While it’s useful to know how to convert numbers from hex to binary, this can be a labor-intensive process doing one number after another. Given that, you should consider using one of these online hex-to-binary converters:
Hexadecimal & Binary Number System Resources
Now that you know the basics of converting hexadecimal numbers to their binary equivalents and vice versa, you may want to know more about how each of these numbering systems work in relationship to each other, especially in terms of computing. To that end, here are some recommended resources to check out:
- SparkFun: This extensive hexadecimal tutorial covers the basics of using hex as well as converting to and from both binary and decimal numbers and provides a handy conversion calculator.
- Tutorials Point: This tutorial shows how to add and subtract using hexadecimal numbers.
- Grinnell College Math Department: Topics covered include binary number theory, negation in the binary system, converting from binary to decimal, and performing addition, subtraction, multiplication, and division using binary numbers.
Remember: Whether you convert from hex to binary by hand or use a converter, understanding how these numbering systems work and their use with computers is key for every programmer.
Want to learn more about using arrays in Java? Check out our guide which covers one- and two-dimensional arrays as well as the methods you should use with them.