4

How can I convert a number from one base, $b_1 \neq 10$ to another base $b_2 \neq 10$ without going through base $10$ i.e. $b_1\rightarrow 10 \rightarrow b_2$?

MJD
  • 65,394
  • 39
  • 298
  • 580

2 Answers2

9

Short answer: You can do it, but you have to do arithmetic in base $b_1$. If you're using a computer, it's easy. If you are using pencil-and-paper, it may be easier to convert through base 10.


The algorithm to convert a number $x$ to base $b$ is:

  1. Set $n = 0$
  2. Divide $x$ by $b$, yielding a quotient $q$ and a remainder $r$
  3. Digit $r_n$ of the answer is $r$
  4. If $q = 0$, halt; the answer is $r_{n}r_{n-1}\ldots r_0$.
  5. Set $x = q$, $n = n+1$ and return to step 2

Let's say you want to convert 1e6 (base 17) to base 7.

We set $x = $ 1e6 and $n=0$. We divide $x$ by 7, yielding a quotient of 48 and a remainder of 1, so $r_0 = 1$ and return to step 2.

Now we divide 48 by 7, yielding a quotient of a and a remainder of 6, so $r_1 = 6$ and we return to step 2.

Now we divide a by 7 yielding a quotient of 1 and a remainder of 3, so $r_2 = 3$ and we return to step 2.

Now we divide 1 by 7 yielding a quotient of 0 and a remainder of 1, so $r_3 = 1$ and since $q=0$ we halt.

The answer is $1361_7$.

MJD
  • 65,394
  • 39
  • 298
  • 580
  • but this is because the computer can do mathematical operation in base $16$. Can I do the inverse of what you did with a computer i.e. converting from base $7$ to base $16$ without going through decimal? –  Feb 25 '13 at 14:33
  • I didn't use a computer; I did it by hand using my brain and a piece of paper. I will redo the example converting from base 17 so that you are not confused. – MJD Feb 25 '13 at 14:37
  • sorry, my bad. How did you divide 1e7 by 7 in base $16$? –  Feb 25 '13 at 14:41
  • I used the usual short division algorithm for base 10, but in base 16 instead. 1e divided by 7 is 4 with a remainder of 2; 47 divided by 7 is a with a remainder of 1, so the quotient is 4a and the remainder is 1. – MJD Feb 25 '13 at 14:43
  • @F'OlaYinka This answer has some discussion of pencil-and-paper arithmetic in bases other than 10. – MJD Feb 25 '13 at 14:55
  • Thank you, you've been of great help. –  Feb 25 '13 at 14:59
  • In this answer, does "the quotient q" refer to the quotient with the remainder, or the quotient without the remainder? – Anderson Green Apr 02 '13 at 00:49
  • 1
    It means the quotient without the remainder. The remainder is $r$. – MJD Apr 02 '13 at 02:14
2

If you can do arithmetic in base $b_1$, you can use the technique of repeatedly dividing by $b_2$ and reading off the remainders in reverse order. For example to convert $261_{\text{seven}}$ to base four, you can carry out the following calculation (which is entirely in base seven):

$$\begin{align*} 261&=4\cdot50+1\\ 50&=4\cdot11+3\\ 11&=4\cdot2+0\\ 2&=4\cdot0+2 \end{align*}$$

Thus, $261_{\text{seven}}=2031_{\text{four}}$.

To see why it works, imagine that we’ve already written the number in base four. The remainder after division by four is just the unit’s (= least significant) digit, and the integer quotient is what’s left when that digit is removed.

For the reverse conversion, done entirely in base four:

$$\begin{align*} 2031&=13\cdot110+1\\ 110&=13\cdot2+12\\ 2&=13\cdot0+2 \end{align*}$$

Of course the middle digit has to be written $6$ in base seven instead of the base four $12$, but we get $2031_{\text{four}}=261_{\text{seven}}$, as expected.

Brian M. Scott
  • 616,228