Pages

Sunday 19 May 2013

Numeral Systems Conversion : Binary, Octal, Decimal and Hexadecimal


binary system, convert numeral systems, convert to binary, convert to hex, convert to hexadecimal, decimal system, hexadecimal system, learn the numeral systems, numeric systems, octal system

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 :
  1. 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 :

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

101101,0010111(2)->Χ(10)

Index the digits of the number

150413120110,0-10-21-30-41-51-61-7

Multiply each digit

1 * 25 + 0 * 24 + 1 * 23 + 1 * 22 + 0 * 21 + 1 * 20 + 0 * 2-1 + 0 * 2-2 + 1 * 2-3 + 0 * 2-4 + 1 * 2-5 + 1 * 2-6 + 1 * 2-7 =

(β-α = 1/βα)

32 + 0 + 8 + 4 + 0 + 1 + 0 + 0 + 0,125 + 0 + 0,03125 + 0,015625 + 0,007813

= 45,179688(10)

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

55,135136(8)->Χ(10)

Index the digits of the number

5150,1-13-25-31-43-56-6

We multiply each digit

5 * 81 + 5 * 80 + 1 * 8-1 + 3 * 8-2 + 5 * 8-3 + 1 * 8-4 + 3 * 8-5 + 6 * 8-6 =

40 + 5 + 0,125 + 0,03125 + 0,009766 + 0,000244 + 0,0001 + 0,0000229

= 45,1663829(10)

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





2D,2E978D(16)->Χ(10)

Index the digits of the number

21130,2-114-29-37-48-513-6

We multiply each digit

2 * 161 + 13 * 160 + 2 * 16-1 + 14 * 16-2 + 9 * 16-3 + 7 * 16-4 + 8 * 16-5 + 13 * 16-6 =

32 + 13 + 0,125 + 0,0546875 + 0,00219727 + 0,00010681 + 0,00000762 + 0,00000077

= 45,18199997(10)

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

No comments:

Post a Comment

Search This Blog

Thank You For Visiting Fun And Education

ko0l fun expt