Drexel dragonThe Math ForumDonate to the Math Forum

Ask Dr. Math - Questions and Answers from our Archives
_____________________________________________
Associated Topics || Dr. Math Home || Search Dr. Math
_____________________________________________

Converting Bases


Date: 9/5/96 at 23:52:38
From: David & Pat Blanton
Subject: Converting Bases

Hello

I am taking CIS courses and we need to learn the art of converting 
from one base to another - i.e. decimal to bin, hex, and oct.

Do you have any advice?

Thank you,
Pat


Date: 10/18/96 at 17:41:1
From: Doctor Kate
Subject: Re: Converting Bases

There is a trick you can use, and after a little practice you won't 
have to write down the steps any more.  Let's go from base 10 (decimal) 
to base 16 (hex) with the number 3457 (decimal):

  3457 = ? * 16 + ?

You divide by 16, and the remainder takes the part of the second ? , 
so:

  3457 = 216 * 16 + 1

Now repeat using the 216 as your decimal number:

3457 = 216 * 16 + 1   start with 3457 as your decimal number
 216 =  13 * 16 + 8   here take the 216 as your decimal no. for the line
  13 =   0 * 16 + 13  here you take the 13 down as your decimal number

Now you have a 0 multiplied by 16 in the last line, so you stop.

Since 13 isn't a digit, we'll write it as 'D'

3457 in decimal = D81 in hex (base 16).  Notice how I read up the last 
column to get this.

You can do this with any change of base.  With base 2 (binary), it 
will take a lot of lines: just keep going until you have 0 in the 
second column (here, we had 0*16). If you take some time to examine 
this, you will start to see why it works.

Hope this helps,

-Doctor Kate,  The Math Forum
 Check out our web site!  http://mathforum.org/dr.math/   


Date: 10/19/2001 at 08:36:52
From: Doctor Peterson
Subject: Re: Conversion between bases

In general there is no need to convert directly between bases, and no 
really convenient way to do so. It can be done, but I don't know why
anyone would bother. (There are special cases, such as where one base is
a power of the other; programmers routinely convert directly between
binary and hexadecimal with hardly any thought.) I'd normally leave it
there, but since I see people ask how to convert directly from base 6 to
base 3 without using base 10, this time I'll take up the challenge and
see if I can do it.

The problem is that in order to do a conversion, you have to do the 
arithmetic in some base; we have a choice of base 3, 6, or 10. When we 
convert from decimal to a less familiar base, we use a method that lets
us work in the source base, such as this to convert 76 (base 10) to base
3:

                     2 2 1 1 (base 3)
                     ^ ^ ^ ^
                     | | | |
           0 rem 2 --+ | | |
         ----          | | |
       3 ) 2 rem 2 ----+ | |
        -----            | |
      3 )  8 rem 1 ------+ |
       ------              |
     3 )  25 rem 1 --------+
      -------
    3 )   76 (base 10)

Here I started with 76 (base 10) and did a series of divisions in base
10, then turned the base 10 remainders into base 3 digits.

To convert from base 6 to base 3, I can use this same method as long as I
know how to divide in base 6. It can be done, but it's certainly awkward:

                     2 2 1 1 (base 3)
                     ^ ^ ^ ^
                     | | | |
           0 rem 2 --+ | | |
         ----          | | |
       3 ) 2 rem 2 ----+ | |
        -----            | |
      3 ) 12 rem 1 ------+ |
       ------              |
     3 )  41 rem 1 --------+
      -------
    3 )  204 (base 6)

In doing this, I'm likely to be thinking in base 10 anyway!

Similarly, when we convert TO decimal, we use a method that lets us work
in the target base, such as this to convert 204 (base 6) to 
base 10:

            2 0 4 (base 6)
            | | |
      2 <---+ | |
    * 6       | |
    ---       | |
     12       | |
    + 0 <-----+ |
    ---         |
     12         |
    * 6         |
    ---         |
     72         |
    + 4 <-------+
    ---
     76 (base 10)

This time I did a series of multiplications (in base 10) by the source 
base, 6, adding in digits from the base 6 number (taken as base 10
numbers).

Again, I can use this method to convert from base 6 to base 3, but I have
to work in base 3, in this case rewriting single digits in base 6 as
(sometimes two-digit) numbers in base 3, and writing the source base, 6,
as 20 (base 3):

            2 0 4 (base 6)
            | | |
       2 <--+ | |
    * 20      | |
    ----      | |
     110      | |
    +  0 <----+ |
    ----        |
     110        |
    * 20        |
    ----        |
    2200        |
    + 11 <------+
    ----
    2211 (base 3)

Using either method, we handle the number itself at some point (as input
or as output) IN THE BASE WE'RE WORKING IN, so we have to work in either
the source base or the target base, unless we convert to decimal. I can't
imagine any method that would let us work in decimal while never writing
the number itself in decimal.

Now, I mentioned special cases at the top. Since 6 is a multiple of 3,
this is actually somewhat special, and we can do the conversion directly.
I don't know that there is a general technique, but we can just expand
the number and see what's happening:

    204 (base 6) = 2 * 6^2 + 0 * 6 + 4 * 1
                 = 2 * (2*3)^2 + 0 * (2*3) + 4 * 1
                 = (2*2^2) * 3^2 + (0*2) * 3 + 4 * 1
                 = 8 * 3^2 + 0 * 3 + 4 * 1
                 = (2*3 + 2) * 3^2 + 0 * 3 + (1*3 + 1) * 1
                 = 2 * 3^3 + 2 * 3^2 + 1 * 3 + 1 * 1
                 = 2211 (base 3)

Is that a direct conversion, or does that fact that I thought in base 10
make it invalid?

- Doctor Peterson, The Math Forum
  http://mathforum.org/dr.math/   
    
Associated Topics:
High School Number Theory

Search the Dr. Math Library:


Find items containing (put spaces between keywords):
 
Click only once for faster results:

[ Choose "whole words" when searching for a word like age.]

all keywords, in any order at least one, that exact phrase
parts of words whole words

Submit your own question to Dr. Math

[Privacy Policy] [Terms of Use]

_____________________________________
Math Forum Home || Math Library || Quick Reference || Math Forum Search
_____________________________________

Ask Dr. MathTM
© 1994-2013 The Math Forum
http://mathforum.org/dr.math/