










Code Conversion



Converting from one code form to another code form is called code conversion, like converting from binary to decimal or converting from hexadecimal to decimal. 





BinaryToDecimal Conversion



Any binary number can be converted to its decimal equivalent simply by summing together the weights of the various positions in the binary number which contain a 1. 





Binary

Decimal

11011_{2}


2^{4}+2^{3}+0^{1}+2^{1}+2^{0}

=16+8+0+2+1

Result

27_{10}







and 





Binary

Decimal

10110101_{2}


2^{7}+0^{6}+2^{5}+2^{4}+0^{3}+2^{2}+0^{1}+2^{0}

=128+0+32+16+0+4+0+1

Result

181_{10}







You should have noticed that the method is to find the weights (i.e., powers of 2) for each bit position that contains a 1, and then to add them up. 





DecimalToBinary Conversion






There are 2 methods: 





 Reverse of BinaryToDecimal Method
 Repeat Division






Reverse of BinaryToDecimal Method






Decimal

Binary

45_{10}

=32 + 0 + 8 + 4 +0 + 1


=2^{5}+0+2^{3}+2^{2}+0+2^{0}

Result

=101101_{2}










Repeat DivisionConvert decimal to binary



This method uses repeated division by 2. 





Convert 25_{10} to binary 





Division

Remainder

Binary

25/2

= 12+ remainder of 1

1 (Least Significant Bit)

12/2

= 6 + remainder of 0

0

6/2

= 3 + remainder of 0

0

3/2

= 1 + remainder of 1

1

1/2

= 0 + remainder of 1

1 (Most Significant Bit)

Result

25_{10}

= 11001_{2}







The Flow chart for repeateddivision method is as follows: 

















BinaryToOctal / OctalToBinary Conversion






Octal Digit

0

1

2

3

4

5

6

7

Binary Equivalent

000

001

010

011

100

101

110

111







Each Octal digit is represented by three binary digits. 





Example: 


100 111 010_{2} = (100) (111) (010)_{2} = 4 7 2_{8} 





Repeat DivisionConvert decimal to octal






This method uses repeated division by 8. 





Example: Convert 177_{10} to octal and binary 





Division

Result

Binary

177/8

= 22+ remainder of 1

1 (Least Significant Bit)

22/ 8

= 2 + remainder of 6

6

2 / 8

= 0 + remainder of 2

2 (Most Significant Bit)

Result

177_{10}

= 261_{8}

Binary


= 010110001_{2}







Hexadecimal to Decimal/Decimal to Hexadecimal Conversion






Example: 


2AF_{16} = 2 x (16^{2}) + 10 x (16^{1}) + 15 x (16^{0}) = 687_{10} 





Repeat Division Convert decimal to hexadecimal



This method uses repeated division by 16. 





Example: convert 378_{10} to hexadecimal and binary: 





Division

Result

Hexadecimal

378/16

= 23+ remainder of 10

A (Least Significant Bit)23

23/16

= 1 + remainder of 7

7

1/16

= 0 + remainder of 1

1 (Most Significant Bit)

Result

378_{10}

= 17A_{16}

Binary


= 0001 0111 1010_{2}







BinaryToHexadecimal /HexadecimalToBinary Conversion






Hexadecimal Digit

0

1

2

3

4

5

6

7

Binary Equivalent

0000

0001

0010

0011

0100

0101

0110

0111







Hexadecimal Digit

8

9

A

B

C

D

E

F

Binary Equivalent

1000

1001

1010

1011

1100

1101

1110

1111







Each Hexadecimal digit is represented by four bits of binary digit. 





Example: 





1011 0010 1111_{2} = (1011) (0010) (1111)_{2} = B 2 F_{16} 





OctalToHexadecimal HexadecimalToOctal Conversion






 Convert Octal (Hexadecimal) to Binary first.
 Regroup the binary number by three bits per group starting from LSB if Octal is required.
 Regroup the binary number by four bits per group starting from LSB if Hexadecimal is required.






Example: 





Convert 5A8_{16} to Octal. 





Hexadecimal

Binary/Octal

5A816

= 0101 1010 1000 (Binary)


= 010 110 101 000 (Binary)

Result

= 2 6 5 0 (Octal)
















