Conversions Between Number Bases

It is sometimes convenient to use a number in a base different from the base currently being used -- that is, to change the number from one base to another.  Many programmers can nimbly convert a number from one base to another, among bases 10, 2, 8 and 16.  There is an important and relatively simple conversion technique involved.

To Base 10 from Bases 2, 8, and 16

First consider what is known as positional notation.  Positional notation means that the value of a digit in a number depends not only on its own intrinsic value but also on its location in the number.  Given the number 2363, we know that the appearance of the digit 3 represents two different values, 300 and 3.  The following table shows the values for such a 4-digit number (base 10):
 
Digit 4th 3rd 2nd 1st (rightmost)
Position thousand hundred ten unit

Using these positional values, the number 2363 is understood to mean
       2000
         300
           60
             3
       ------
       2363

This number can also be expressed as:
       (2 x 1000) + (3 x 100) + (6 x 10) + 3

We can express this expanded version of the number another way, using powers of 10.  Note that 100 = 1

       2363 = (2 x 103) + (3 x 102) + (6 x 101) + (3 x 100)

Once you understand the expanded notation, the rest is easy:  You expand the number just as in base 10, but use the appropriate base of the number.

Example 1: follow the steps to convert 617328 to base 10:

step 1.  Expand the number, using 8 as the base:

       61732 = (6 x 84) + (1 x 83) + (7 x 82) + (3 x 81) + (2 x 80)

step 2.  Complete the arithmetic:

       61732 = (6 x 4096) + (1 x 512) + (7 x 64) + (3 x 8) + (2 x 1)
                  = 24576 + 512 + 448 + 24 + 2

step 3.  Answer:  617328 = 2556210

The same expand-and-convert technique can be used to convert form base 2 or base 16 to base 10.  As you consider the following two examples, use this table:

Table C: Number bases 10, 2, 8, 16 (first 16 values)
 
Base 10 Base 2 Base 8 Base 16
(decimal) (binary) (octal) (hexadecimal)
0 0000 0 0
1 0001 1 1
2 0010 2 2
3 0011 3 3
4 0100 4 4
5 0101 5 5
6 0110 6 6
7 0111 7 7
8 1000 10 8
9 1001 11 9
10 1010 12 A
11 1011 13 B
12 1100 14 C
13 1101 15 D
14 1110 16 E
15 1111 17 F
16 10000 20 10

Example 2: convert C14A16 to base 10:

C14A16 = (12 x 163) + (1 x 162) + (4 x 161) + 10 x 160)

              = (12 x 4096) + (1 x 256) + (4 x 16) + (10 x 1) = 49482

So C14A16 = 4948210

Example 3: convert 1001112 to base 10:
1001112 = (1 x 25) + (1 x 22) + (1 x 21) + ( 1 x 20)   [notice that you skip 0 x 2n for n = 3, 4]

               = 39

So 1001112 = 3910

From Base 10 to Bases 2, 8, and 16

These conversions use a simpler process but more complicated arithmetic.  The process, often called the remainder method, is basically a series of repeated divisions by the number of the base to which you are converting.  You begin by using the number to be converted as the dividend; succeeding dividends are the quotients of the previous division.  The converted number is the combined remainders accumulated from the divisions.  There are two points to remember:
1) keep dividing until you reach a zero quotient.
2) use the remainders in reverse order.

Consider converting 695410 to base 8 (remainders shown in red):

8 | 6954
8 | 869     2
8 | 108     5
8 | 13      4
8 | 1      5
0         1

Placing the remainders backward, 695410 = 154528

Convert 482310 to base 16:

16 | 4823
16 | 3017    7
16 | 18        13 (= D)
16 | 1          2
0                 1

The remainder 13 is equivalent to D in base 16.  So 482310 = 12D716.

Convert 4910 to base 2:

2 | 49
2 | 24    1
2 | 12    0
2 | 6      0
2 | 3      0
2 | 1      1
0           1

Again placing the remainders in reverse order, 4910 = 1100012.

To Base 2 from Bases 8 and 16.

To convert a number to base 2 from base 8 or 16, convert each digit separately to three or four binary digits, respectively.  Use table C above to make the conversion.  Leading zeros - zeros added to the front of the number - may be needed in each grouping of digits to fill out each to three or four digits.

Converting 47328 to base 2, converting each octal digit to a set of three binary digits:

  4           7           3        2

100       111       011    010

So 47328 = 1001110110102.  Notice that leading zeros were sometimes needed to make three binary digits from an octal digit: for octal digit 3, 11 became 011 and, for octal digit 2, 10 became 010.

Convert A046B16 to base 2, this time converting each hexadecimal digit to four binary digits:

   A            0             4              6             B
1010       0000       0100        0110       1011

Thus A046B16 = 101000000100011010112.

From Base 2 to Bases 8 and 16.

To convert a number from base 2 to base 8 or 16, group the binary digits from the right in groups of three or four, respectively.  Again use table C to help make the conversion to the new base.

Convert 1111010010112 to base 8 and base 16:

In the base 8 conversion, group the digits three at a time, starting on the right:

111   101   001   011
  7       5       1       3

So 1111010010112 = 75138.

For the conversion to base 16, group the digits four at a time, starting on the right:

1111   0100   1011
   F         4         B

1111010010112 = F4B16.

Sometimes the number of digits in a binary number is not exactly divisible by 3 or 4.  You may, for example, start grouping the digits three at a time and finish with one or two "extra" digits on the left side of the number.  In this case simply add as many zeros as you need to front (left) of the binary number.

Consider converting 10102 to base 8.  By adding two zeros to the front of the number to make it 0010102, we now have six digits, which can be conveniently grouped three at a time.  With this change, 0010102 can be easily converted to 128.

Summary Table for Conversions: Note: converting base 8 to base 16 and base 16 to base 8 involve two steps (also, these conversions are not commonly needed).
 
 
to base 2 to base 8 to base 16 to base 10
From base 2
-
Group binary digits by 3, convert. Group binary digits by 4, convert. Expand number and convert base 2 digits to base 10.
From base 8 Convert each octal digit to 3 binary digits.
-
Convert to base 2, then to base 16. Expand number and convert base 8 digits to base 10.
From base 16 Convert each hexadecimal digit to 4 binary digits Convert to base 2, then to base 8.
-
Expand number and convert base 16 digits to base 10.
From base 10 Divide number repeatedly by 2; use remainders as answer. Divide number repeatedly by 8; use remainders as answer. Divide number repeatedly by 16; use remainders as answer.
-