It is useful to understand how to convert between binary, decimal and hexadecimal, even though you’ll probably have your favorite online conversion tool bookmarked already. In this post I will explain to you how you can do the conversions manually.
The “normal” numbering system we know, also known as the decimal numbering system, uses the base 10 system. This means that we can use the numbers 0 through 9. We start with 0 and go on until 9, and when we increment the number the number, we start with 0 again, but add a 1 to the left.
0 1 2 3 .. 10 11 12 13 .. 20 21 22 23 .. 100 101 102 103 .. 1000 1001 1002 1003 etcetera..
When you write digits next to each other, they will have different values. The exact value of the number is determined by the base of the system. The methodology used for this is called the Place Value System. We can create a Place Value Chart to list the value of each place in a numbering system.
With the base 10 numbering system, we start with 1. When we want to get the value of the position immediately left to the 1, we multiply this by 10. If we then want to know the value of the next digit left to the previous one, we again multiply by 10. This goes on indefinitely. The following image will make this clear.
We can also use the system of powers to write this out. This looks as follows:
Using the system of powers is useful, because we can make a place value chart of any base system, without having to change the powers. You can immediately see that we use the base 10 system, because every number is a 10 to the power of.
I’ve already partly covered binary theory in the post How do IP addresses work?, but since that post was mainly focused on using binary to calculate with IP addresses, it does not fully cover all theory on binary conversion.
If you’ve already read the above mentioned post, you’ll already know the following part, but let’s recap the way the binary numbering system is build up.
The binary numbering system uses the base 2 system. Meaning you only have a 0 and a 1. The concept of adding up numbers is still the same in binary as with decimal. You start with a 0 and add a 1. Since there are no more numbers after that, we start with a 0 again, but add a 1 to the left. Now you get the following:
0 1 10 11 100 101 111 1000 1001 1010 1011 etcetera
Recalling the place value system used for the binary numbering system, we can also make a place value chart for binary.
If you understand binary because you learned it for understanding how IP addresses work, you usually stop at 27 because one octet of an IP address is 8 bits long. However, 213 for example, is also a valid binary number, representing 8192 in decimal. This brings us on the topic of converting decimal numbers to binary.
Converting decimal to binary
Let’s pick the random number 5,932. There are two ways of converting decimal to binary. I’ll explain the version which is the most scalable for use with larger numbers.
To start, you divide the number you want to write in binary by 2. You then keep dividing by using the answer of the previous calculation until you reach 0. You only write down the whole numbers but keep track of the remainder when you don’t have a whole number as an answer. We also don’t round these numbers up or down.
Hold on, we’re obviously not there yet. To know how to write this in binary, you start from the bottom most answer, and write a 1 when the answer had a remainder and write a 0 when the answer was a whole number.
5,932 / 2 = 2966 0 2,966 / 2 = 1483 0 1,483 / 2 = 741 remainder 1 741 / 2 = 370 remainder 1 370 / 2 = 185 0 185 / 2 = 92 remainder 1 92 / 2 = 46 0 46 / 2 = 23 0 23 / 2 = 11 remainder 1 11 / 2 = 5 remainder 1 5 / 2 = 2 remainder 1 2 / 2 = 1 0 1 / 2 = 0 remainder 1
Remember, write down your answer from the bottom up, and then you’ll have the binary equivalent of the number 5932.
Converting binary to decimal
If you want to convert any binary number to decimal, you will have the make use of the place value chart to map out the value of each digit in relation to its position. After you’ve marked the values, you just have to add them all up. This is exactly the same method you would use to calculate IP addresses, but in this context we’re not limited to 8 bits per octet. Let’s take for instance the binary number 1010100011110.
On the right side of a binary number sits the LSB, the least significant bit. This just means that that’s the digit that has the lowest value. On the left side you have the MSB, the most significant bit. This digit has the highest value. From a human standpoint, this means you have to read a binary number from the right to the left in order to make something out of it.
In this binary number, we have 13 positions. Let’s make the place value chart and immediately under that write down the binary numbers on their respective positions.
Now we can make just add up the places which have a 1 in the field. This will make a total of 5406. Go grab your favorite online conversion tool and see if i’m right. 🙂
Up until now, we haven’t dealt with hexadecimal yet. The hexadecimal numbering system uses a base 16 system. Since this is higher than the decimal numbering system, we have to use other characters to represent numbers that are higher than 10. Therefore, the following has been created, from lowest to highest:
0 1 2 3 4 5 6 7 8 9 a b c d e f
The A through F represent 10 through 15. Let’s create a place value chart for hexadecimal.
With using just 6 positions, we can see how big these numbers already are, whereas with binary the answer to 25 is only 32. This brings us to the reason of the existence of hexadecimal. It is used to make our lives a little bit easier and make large numbers more human readable, by presenting them in a different way.
IPv6 addresses and also MAC addresses, are some common examples of hexadecimal representations. Memory allocations are also represented as hexadecimal, and I’ve even seen examples where IPv4 addresses and TCP/IP port numbers were represented as hexadecimal numbers!
Converting decimal to hexadecimal
As with the conversion from decimal to binary, we will also use the division method, since that method is scalable with larger numbers. The steps to convert a decimal number differ from decimal to binary though.
Let’s grab a random number. Say, 45,162. We’ll make a table below to write down the different answers and steps.
- We start by dividing that number by the base of the system, in this case 16.
- We remove anything after the decimal point for the answer in the Result column.
- To get the remainder, we multiply the value after the decimal point (if there is any!) by the base. I.e.: 0.625 * 16 = 10. You write this down in hexadecimal, so 10 is A. You can also use the Modulus on your calculator for this. I.e.: 45162 MOD 16 = 10. You use the modulus on the previous answer to get the result for the current division
- You keep dividing until you reach 0, at which point division is no longer possible.
|Division||Result||Remainder (write in hex)|
|45162 / 16||2822||A (in decimal 10, but you write it in hex)|
|2822 / 16||176||6 (2822 MOD 16)|
|176 / 16||11||0 (there is no value after the decimal point, so this is 0)|
|11 / 16||0||B|
We have now filled the table. To write the hexadecimal answer of this conversion, we start from the bottom up. The answer to the conversion of 45,162 to hexadecimal is therefore B06A.
Convert hexadecimal to decimal
In order to convert hexadecimal numbers to decimal, we will make use of the place value chart for base 16 and calculate each value according to its position in the place value chart. The process is much the same as with binary to decimal, which the addition that you have actual values to calculate, instead of it being just a 1 or 0.
Let’s pick a random hexadecimal number. For instance, 4B7E2A. Since this number is 6 positions long, we can make a place value chart of 6 positions. We now have 3 rows, where we write the hexadecimal number (in decimal!) according to its position.
|4||11 (B)||7||14 (E)||2||10 (A)|
We now calculate the decimal values for each position as follows:
4B7E2A = 165 x 4 + 164 x 11 + 163 x 7 + 162 x 14 + 161 x 2 + 160 x 10
This sum can then also be written as:
4B7E2A = 4194304 + 720896 + 28672 + 3584 + 32 + 10
The answer for the conversion of 4B7E2A to decimal is therefore 4,947,498. you will definitely need a calculator for these types of calculation, but it’s good to know how to do these types of conversions manually, even though you’re using a calculator.