Programming


Converting Between Integer 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
DecimalHexadecimalBinaryOctal
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, respectively), 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*256) + (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. The last remainder is the most significant bit, and the first remainder is the least significant bit.

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


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 decimal 16 and noting the remainders. The first remainder is the least significant digit and the last remainder is the most significant digit.

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 number into a binary, then convert the binary into a hexadecimal number. To change the representation of the binary number to hexadecimal, separate the digits into 4-bit groups beginning with the least significant bit. Then 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
(base 2)
   1    7 (base 16)


Converting from Decimal to Octal

To convert a decimal number into an octal number, divide it by 8 repeatedly and note the remainders. The remainders are the digits of the octal number. The last remainder is the most significant digit, and the first remainder is the least significant digit.

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


Converting between Binary and Hexadecimal

From Binary to Hexadecimal:  To convert a binary number into its hexadecimal form, start by grouping the digits into 4-bit groups. Beginning with the least significant bit (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  

From Hexadecimal to Binary:  To convert a hexadecimal number into a binary, just reverse the above process; starting all the way to the right, convert each digit into a 4-bit binary number.


Converting between Binary and Octal

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

For example,

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

From Octal to Binary:  To convert an octal number into a binary, just reverse the above process; starting all the way to the right, convert each digit into a 3-bit binary number.


[  Index  |  Technical Notes  ]

DISCLAIMER

Page author: Dawn Rorvik (rorvikd@evergreen.edu)
Last modified: 05/20/2003