## Programming

 Converting Between Representations

### Comparison of Representations

System Digits
Binary (Base 2):  0, 1
Octal (Base 8):  0, 1, 2, 3, 4, 5, 6, 7
Decimal (Base 10):  0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Hexadecimal (Base 16):  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
0000000 00000
1010000 00011
2020000 00102
3030000 00113
4040000 01004
5050000 01015
6060000 01106
7070000 01117
8080000 100010
9090000 100111
100A0000 101012
110B0000 101113
120C0000 110014
130D0000 110115
140E0000 111016
150F0000 111117
16100001 000020
17110001 000121
18120001 001022
. . .. . .. . .. . .
255FF1111 1111377

### Converting from either Hexadecimal, Binary or Octal to Decimal

A binary, a hexadecimal or an octal number can be expressed as the sum of the successive powers of the base (either 2, 16 or 8), with the coefficients being the digits.

For example,

 Decimal 1111(base 10)  = 1     1000(103) 1     100(102) 1     10(101) 1     1(100) =  (1*1000) + (1*100) + (1*10) + (1*1)  =  1111(base 10) Hexadecimal 1111(base 16)  = 1     4096(163) 1     256(162) 1     16(161) 1     1(160) =  (1*4096) + (1*512) + (1*16) + (1*1)  =  4369(base 10) Binary 1111(base 2)  = 1     8(23) 1     4(22) 1     2(21) 1     1(20) =  (1*8) + (1*4) + (1*2) + (1*1)  =  15(base 10) Octal 1111(base 8)  = 1     512(83) 1     64(82) 1     8(81) 1     1(80) =  (1*512) + (1*64) + (1*8) + (1*1)  =  585(base 10)

### Converting from Decimal to Binary

To convert a decimal number into a binary number, divide it by 2 repeatedly and note the remainders. The remainders are the bits of the binary number.

For example,

 13(base 10) = 1101(base 2) 13 ÷ 2 = 6 remainder 1 LSB 6 ÷ 2 = 3 remainder 0 3 ÷ 2 = 1 remainder 1 1 ÷ 2 = 0 remainder 1 MSB NOTE: that the last remainder is the MSB, and the first remainder is the LSB.

### Converting from Decimal to Hexadecimal

There are two different ways to convert a decimal number into a hexadecimal number.

1.  The first method is similar to converting a decimal to a binary and involves dividing the number by 16 and noting the remainders.

For example,

 4620(base 10) = 120C(base 16) 4620 ÷ 16 = 288 remainder 12 = C LSB 288 ÷ 16 =  18 remainder 0 18 ÷ 16 =   1 remainder 2 1 ÷ 16 =   0 remainder 1 MSB

2.  The second method involves converting the decimal into a binary, then convert the binary into a hexadecimal number by grouping the digits into 4-bit groups. Beginning with the LSB, write the hexadecimal equivalent of each group.

For example,

 23(base 10) = 17(base 16) 23(base 10) = 0001 0111(base 2) 23 ÷ 2 = 11 remainder 1 LSB 11 ÷ 2 =  5 remainder 1 5 ÷ 2 =  2 remainder 1 2 ÷ 2 =  1 remainder 0 1 ÷ 2 =  0 remainder 1 MSB

 0001 0111(base 2) = 17(base 16) 0001 0111 1 7

### Converting from Decimal to Octal

To convert an octal number into a binary number, divide it by 8 repeatedly and note the remainders. The remainders are the bits of the binary number.

For example,

 1701(base 10) = 3245(base 8) 1701 ÷ 8 = 212 remainder 5 LSB 212 ÷ 8 =  26 remainder 4 26 ÷ 8 =   3 remainder 2 3 ÷ 8 =   0 remainder 3 MSB NOTE: that the last remainder is the MSB, and the first remainder is the LSB.

### Converting from Binary to Hexadecimal and Vice Versa

To convert a binary number into its hexadecimal form, start by grouping the digits into 4-bit groups. Beginning with the LSB (all the way to the right of the number), write the hexadecimal equivalent of each group.

For example,

 1001111101100101(base 2) = 9F65(base 16) 1001 1111 0110 0101 (base 2) 9 F 6 5 (base 16) MSB LSB

To convert a hexadecimal into a binary number, just reverse the above process; beginning with the LSB, convert each digit into a 4-bit binary number.

### Converting from Binary to Octal and Vice Versa

To convert a binary number into its octal form, start by grouping the digits into 3-bit groups. Beginning with the LSB (all the way to the right of the number), write the octal equivalent of each group.

For example,

 10110111011(base 2) = 4672(base 8) 010 110 111 011 (base 2) 2 6 7 3 (base 8) MSB LSB

To convert an octal into a binary number, just reverse the above process; beginning with the LSB, convert each digit into a 3-bit binary number.

[  Index  |  Technical Notes Page  ]