3

Is there any kind of method to move between bases?

I know that there are methods to convert between base x to base y, but I'm looking for something general which won't limit me. Is it possible? Maybe there is some kind of equation.

rschwieb
  • 153,510

2 Answers2

3

There are at least two different algorithms you might want to use for converting a number $n$ from base $x$ to base $y$, depending on whether you'd rather do your arithmetic in base $x$ or base $y$.

If you're more comfortable doing arithmetic in your starting base:

Write $n=ay+b$ where $0 \leq b < y$ (that is, do "division with remainder" on $n$). Then $b$ will be the rightmost digit of your new representation of $n$, and you repeat the process on $a$ to get all the other digits. Stop when $a$ is less than $y$ (at which point $a$ will be the leftmost digit of your number).

For example, say we want to write the base-ten number $23504$ in base $7$. Then:

  • $23504$ divided by $7$ is $3357$ with a remainder of $5$, so the rightmost digit is $5$.
  • $3357$ divided by $7$ is $479$ with a remainder of $4$, so the next-to-rightmost digit is $4$.
  • $479$ divided by $7$ is $68$ with a remainder of $3$, so the next digit is a $3$.
  • $68$ divided by $7$ is $9$ with a remainder of $5$, so the next digit is a $5$.
  • $9$ divided by $7$ is $1$ with a remainder of $2$, so the two leftmost digits are $12$.

Putting all this together, we have $23504_{\mathrm{ten}}=125345_7$.

If you're more comfortable doing arithmetic in your ending base:

Start at the left end of your number and keep a running total. As long as you still have more digits, multiply your running total by $x$ and add the next digit to it. When you run out of digits, your running total is the converted number.

For example, say we want to write the base-$7$ number $125345_7$ in base ten. Then:

  • We haven't added anything yet, so our running total is $0$. Multiply by $7$ and add the leftmost digit $(1)$ to get $1$.
  • Our running total is $1$. Multiply by $7$ and add the next digit $(2)$ to get $9$.
  • Our running total is $9$. Multiply by $7$ and add the next digit $(5)$ to get $68$.
  • Our running total is $68$. Multiply by $7$ and add the next digit $(3)$ to get $479$.
  • Our running total is $479$. Multiply by $7$ and add the next digit $(4)$ to get $3357$.
  • Our running total is $3357$. Multiply by $7$ and add the next digit $(5)$ to get $23504$. Since we're now all out of digits, this is the base-$10$ representation of the number.

So again this demonstrates that $125345_7=23504_{\mathrm{ten}}$ (but starting from the other end).

Finally, maybe you're not comfortable doing arithmetic in either base. If there's some base $z$ you are comfortable doing arithmetic in, that's OK! Just use the second algorithm to convert from base $x$ to base $z$, and the first one to convert from base $z$ to base $y$.

Micah
  • 38,108
  • 15
  • 85
  • 133
1

To change bases you use the division algorithm: for an integer $n$ (in any base) and new base $y$, there exists a quotient $q$ and remainder $r$ such that $$n = qy + r, \qquad 0\leq r < y.$$ You can compute $q$ and $r$ by performing long division of $n$ by $y$ in base $x$.

The remainder $r$ then becomes the least significant digit (ones digit) of the number $n$ in base $y$. Repeat the process, using $q$ for $n$, to find the next digits.

user7530
  • 49,280