Numeral Systems Conversion : Binary, Octal, Decimal and Hexadecimal

As a computer programmer, you need to know about the different numeral systems. In your computer works, there will be lots of times that you will be using numeral systems like the binary, hexadecimal and sometimes octal as well. Thus, it would be a great idea to know how to convert numbers from one numeral system to the other. The digits that all 4 numeral systems use are shown below :

Decimal

Binary

Hexadecimal

Octal

012

3

4

5

6

7

8

9

10

11

12

13

14

15

16

0110

11

100

101

110

111

1000

1001

1010

1011

1100

1101

1110

1111

10000

012

3

4

5

6

7

8

9

A

B

C

D

E

F

10

012

3

4

5

6

7

10

11

12

13

14

15

16

17

20

Convert From Decimal to Other Numeral Systems

To convert a number from decimal to any other numeral system, we follow a standard procedure. We divide the integer part of a number with the base number of the system at which we want the actual conversion to occur. For instance, to convert a number from decimal to binary(base 2), we divide that number with 2. While we make each division, the remainder of the division is the rightmost digit of the resulting number in binary and the result of the division gets redivided with 2 till we reach the division where the result is 0. If the number has a fractional part, this gets multiplied by two. The integer part of the number after the multiplication is held, will be the leftmost digit of our new fractional number in the binary system, right after the comma. The result of the multiplication of 2 with the fractional part of the number gets substracted of the integer part used for the creation of the fractional number in the binary system. This new number gets multiplied with 2 again and then the previous procedure is executed again and again, till the new fractional number becomes 0. If the new fractional number is periodic, we cut and round the resulting number. This may sound a bit confusing, so these are some example conversions :

Convert from decimal to binary Χ(10)->Χ(2)

Integer

45(10)->Χ(2)

Div Quotient Remainder Binary Number (Χ)

45 / 2 22 1 1

22 / 2 11 0 01

11 / 2 5 1 101

5 / 2 2 1 1101

2 / 2 1 0 01101

1 / 2 0 1 101101

45(10)->101101(2)

Fractional Part

0,182(10)->Χ(2)

Div Product Remainder Binary Number (Χ)

0,182 * 2 0,364 0 0,0

0,364 * 2 0,728 0 0,00

0,728 * 2 1,456 1 0,001

0,456 * 2 0,912 0 0,0010

0,912 * 2 1,824 1 0,00101

0,824 * 2 1,648 1 0,001011

0,648 * 2 1,296 1 0,0010111

0,182(10)->0,0010111(2) (After we round and cut the number)

Convert from decimal to octal Χ(10)->Χ(8)

Integer

45(10)->X(8)

Div Quotient Remainder Octal Number (Χ)

45 / 8 5 5 5

5 / 8 0 5 55

45(10)->55(8)

Fractional Part

0,182(10)->Χ(8)

Mul Product Integer Binary Number (Χ)

0,182 * 8 1,456 1 0,1

0,456 * 8 3,648 3 0,13

0,648 * 8 5,184 5 0,135

0,184 * 8 1,472 1 0,1351

0,472 * 8 3,776 3 0,13513

0,776 * 8 6,208 6 0,135136

0,182(10)->0,135136(8) (After we round and cut the number)

Convert from decimal to hexadecimal Χ(10)->Χ(16)

Integer

45(10)->X(16)

Div Quotient Remainder Hex Number (Χ)

45 / 16 2 13 D (Since 13 decimal is D in hexadecimal)

2 / 16 0 2 2D (See the table)

45(10)->2D(16)

Fractional Number

0,182(10)->Χ(16)

Mul Product Integer Binary Number (Χ)

0,182 * 16 2,912 2 0,2

0,912 * 16 14,592 14 0,2Ε

0,592 * 16 9,472 9 0,2Ε9

0,472 * 16 7,552 7 0,2Ε97

0,552 * 16 8,832 8 0,2Ε978

0,832 * 16 13,312 13 0,2Ε978D

0,182(10)->0,2E978D(16) (After we round and cut the number)

2. Convert from other numeral systems to decimal

Now, in order to do the opposite, we have to do some pretty different steps than the previous ones. At first, we count the number of digits that our number to convert consists of starting from 0 and going from right to left when the number is an integer. However, when the number is decimal, then we count the digits of the number right after the comma, starting from left and going to the right, indexing them starting from -1. So, in order to convert a number from one numeral system to decimal, we multiply each digit with a number that has its base on the numeral system that the number is represented at. For instance, for binary that number is 2 and we raise that number to the index that each digit is at. In the end, we add all the resulting numbers and the result is that number in decimal. Take a look at the examples below to understand it better :

Notice that the decimal numbers are not actually the same as their primary values. We have some losses. These losses are due to the rounds and cuts done in the previous results.

3. Convert from binary to hexadecimal to octal

As we all now, 23 results to 8 and 24 gives 16. This reality, believe it or not, will help you understand how to convert from binary to octal and hexadecimal and vice versa. As we said before, 23 gives 8, so in order to convert a binary number to octal, we need 3 digits that will represent a digit of the octal number. Therefore, for a hexadecimal number we need 4 digits to represent a hexadecimal digit. For that reason, we convert a binary number to octal, then separate the binary number into triads or quadruples (when hex), starting from right to left when talking about integers and from left to right when it’s about decimals starting from the comma. If there are digits left in the end that do not make a triple or a quadruple, for the hex system we pad with 0 in front of the number. Then, we substitute each three digit number or four digit binary number with a digit that corresponds to octal when it’s triples or hex when its quadruples. In this conversion, you will be helped from the table above. Here are some examples to understand what was said :

Convert from binary to octal

110101000,101010(2)->X(8)

| 3 | | 3 | | 3 | | 3 | | 3 |

110 101 000 ,101 010

|| || || || ||

\/ \/ \/ \/ \/

6 5 0 , 5 2 (See that in the array 110(2) corresponds to 6(8) )

110101000,101010(2)->650,52(8)

Convert from binary to hexadecimal

110101000,101010(2)->X(16)

| 4 | | 4 | | 4 | | 4 | | 4 |

0001 1010 1000 ,1010 1000

|| || || || ||

\/ \/ \/ \/ \/

1 Α 8 , Α 8

110101000(2)->1Α8,Α8(16)

4. Convert from hexadecimal to octal and binary

This is the exact reverse procedure than the previous one. As previously, each digit of the octal number corresponds to 3 digits of the binary number and 4 digits of the hexadecimal number. When the appropriate triples or quadruples are not completed, we pad with 0 in front of each number :

Convert from octal to binary

650,52(8)->X(2)

6 5 0 , 5 2

|| || || || ||

\/ \/ \/ \/ \/

110 101 000 , 101 010

650,52(8)->110101000,101010(2)

Convert from hexadecimal to binary

1Α8,Α8(16)->X(2)

1 Α 8 , Α 8

|| || || || ||

\/ \/ \/ \/ \/

0001 1010 1000 ,1010 1000

This is a guest post by a good personal friend of mine under the name Black Shadow.

hey ….ds is awsum yar…i never get good marks in number system..but from this i learned number conversion so easily..i love this..god bless u ..n give lotss of knowledge..so that u can share with uss….he he he …m lovin it…:):):)

Nice article. This is basic need to understand the relation between the different number system. How we can convert this number system to other number system.
You can learn programming version of number conversion here.

Abour CoderCaste

Welcome to CoderCaste, a blog devoted in helping novice and seasoned programmers with their daily programming needs. We hope that you have a good time while around here and we will keep on creating fresh and interesting computer programming related content for thirsty developers. Enjoy !