# Numbering System: How to convert hexadecimal, decimal value to binary and vice versa?5 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: Sorry because the quality of this image is not good. I hope you still get the idea.

### 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.

HexBinary
00000
10001
20010
30011
40100
50101
60110
70111
81000
91001
A1010
B1011
C1100
D1101
E1110
F1111

### Example #1

Convert (4E)16 to binary:

(8)16 = (1000)2

(E)16 = (1110)2

So

(8E)16 = (10001110)2

### Example #2

Convert (2A07)16 to binary:

(2)16 = (0010)2

(A)16 = (1010)2

(0)16 = (0000)2

(7)16 = (0111)2

So

(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

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:

00
11
22
33
44
55
66
77
88
99
A10
B11
C12
D13
E14
F15

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.

DivisorBase Ten NumberRemainderHex Equivalent
164000XX
1625000
161510A
16015F 