Numbering System: How to convert hexadecimal, decimal value to binary and vice versa?4 min read
In the previous article, we’ve talked about what is bitwise operation and why it matters in computer science.
Most of the time when we do things with the bitwise operation, we usually have to deal with some terms like a binary (base 2) and hexadecimal (base 16). As you probably know, the binary system consists of just two digits 0 and 1, decimal (base 10) is the values 0 to 9 (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) and hexadecimal (base 16) has the values 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F.
Since binary is the internal language of the computer, so it is crucial that programmers have the ability to convert decimal to binary, hexadecimal to binary and vice versa.
Convert decimal number to binary and vice versa
Convert decimal to binary
Presume we have the value in decimal of 156 (base 10), how do we can convert it into a binary number? First, let’s prepare a paper and a pen because we need them in this situation.
Very simple and easy to approach, we just have to divide this number to 2 consecutively until you reach to 0 and everything you need is wrap all of the reminder bottoms to top to get the binary number. It’s little vague when we just talk, now let’s practice, look at the example below:
Convert binary to decimal
So sometimes the binary number is really cumbersome and hard to understand. For more convenience, occasionally we want to convert binary number to decimal number to do some particular tasks. So how do we do it? Unlike converting decimal to binary we have to divide something, everything we need to convert binary number to decimal number is write down it to paper (again). From right to left, start with 2⁰ from the rightmost multiple by its position digit, then plus with its next digit and increase the exponent by one for each power, finally sum up all the things together, we get the decimal number. It’s so confusing when you read it, so let’s do some example. We will convert 111000110 (base 2) to decimal (base 10).
This image illustrates how we can convert binary number to decimal, you can test this value and also practice by converting it to binary again like the first image.
Convert the hexadecimal number to binary and vice versa
Convert the hexadecimal to binary
In order to convert hexadecimal to binary number, every you need is a table with each value of hex (base 16) corresponding with the value of binary (base 2). For each value of hexadecimal, we write down the corresponded value in base 2 and concat them together.
Convert (4E)16 to binary:
(8)16 = (1000)2
(E)16 = (1110)2
(8E)16 = (10001110)2
Convert (2A07)16 to binary:
(2)16 = (0010)2
(A)16 = (1010)2
(0)16 = (0000)2
(7)16 = (0111)2
(2A07)16 = (0011101000000111)2
Convert binary number to hex number
There are several ways that help you convert binary number to hexadecimal number. The first one is, with the available binary number, you group 4 numbers in a group from right to left and look up the value of these on the table above:
- Pretend we have the value base 2 like this: 1010001011001110.
- Step one, group 4 numbers in a group from right to left (in case the last group does not have enough 4 digits, just add 0 to the left to make this group digit equals to 4). (1010)(0010)(1100)(1110)
- Look upon the table above and write down the respective values from left to right. (1010) = B, (0010) = 2, (1100) = C, (1110) = E
- Then we get the desired result in base 16 is: B2CE
Some other ways almost the same here, so I don’t list them here.
Convert hexadecimal to decimal and vice versa
Convert hexadecimal to decimal
Likewise, the way we convert the base 2 to base 10. We can convert hexadecimal number to decimal number exact steps like binary to decimal, but instead of 2⁰ from the rightmost digit, we need 16⁰. Look at the table below to see the corresponding digits from base 16 to base 10:
So let’s practice now to see how it is:
4DE5F (base 16) to decimal => 4 = 4, D = 13, E = 14, 5 = 5, F = 15
4DE5F = 4 x 16⁴ + 13 x 16³ + 14 x 16² + 5 x 16¹ + 15 x 16⁰ = 319071 (base 10)
Convert decimal to hex
Likewise, the way we convert decimal number to binary number, but instead of dividing the number to 2 until it reaches 0, we divide that number to 16. Look at the examples underneath:
In this example, we want to convert decimal 4000 to a hexadecimal.
|Divisor||Base Ten Number||Remainder||Hex Equivalent|
The answer is FA0 hexadecimal. Remember, we write the last remainder we received at the front of our answer.
Through this article, we’ve learned how to convert decimal to binary, hex to binary, decimal to hex and vice versa. Try to practice more to remember those things. The tables convert number to other type of number just is the computation of those with power of the base you want to convert. We write down binary number from decimal number from bottom to top of the reminder and so the others. In hexadecimal, we have 16 values, A = 10 in base 10 B = 11 in base 10 and so on…