base conversion

The PlanetMath entry bases gives a good overview over the symbolic representation of numbers, something which we use every day. This entry will give a simple overview and method of converting numbers between bases: that is, taking one representation of a number and converting it to another. When dealing with extremely large numbers, base conversion may become quite slow. Fortunately, methods exist which are significantly more efficient than those listed here; see Knuth, The Art of Computer Programming. The simplest of these work by divide-and-conquer, converting groups of digits at once. In this article, we focus on a more naive approach.

Perhaps the simplest way to explain base conversion is to describe the conversion to and from base 10 (in which everyone is accustomed to performing arithmetic). We will begin with the easier method, which is the conversion from some other base to base 10.

Conversion to Base 10

Suppose we have a number represented in base $b$. This number is given as a sequence of symbols $s_{n}s_{n-1}\cdots s_{2}s_{1}.t_{1}t_{2}\cdots t_{m}$. This sequence represents

 $s_{n}b^{n-1}+s_{n-1}b^{n-2}+\cdots+s_{2}b+s_{1}+t_{1}b^{-1}+t_{2}b^{-2}+\cdots% +t_{m}b^{-m}$

This is straight-forward enough. All we need to do is convert each symbol $s$ to its decimal equivalent. Typically this is simple. The symbols $0,1,\cdots,9$ usually represent the same value in any other base. For $b>9$, the letters of the alphabet begin to be used, with $a=10,b=11,$ and so on. This serves up to $b=36$. Since the most common bases used are binary ($b=2$), octal ($b=8$), decimal ($b=10$), and hexadecimal ($b=16$), this scheme is generally sufficient.

Once we map symbols to values, we can easily apply the formula above, adding and multiplying as we go along.

Example of Conversion to Base 10

1. 1.

Binary to Decimal Let $b=2$. Convert 10010.011 to decimal.

 10010.011 $\displaystyle=$ $\displaystyle 1\cdot 2^{4}+0\cdot 2^{3}+0\cdot 2^{2}+1\cdot 2+0+0\cdot 2^{-1}+% 1\cdot 2^{-2}+1\cdot 2^{-3}$ $\displaystyle=$ $\displaystyle 16+2+\frac{1}{4}+\frac{1}{8}$ $\displaystyle=$ $\displaystyle 18.375$
2. 2.

Ternary to Decimal Let $b=3$. Convert 10210.21 to decimal.

 10210.21 $\displaystyle=$ $\displaystyle 1\cdot 3^{4}+0\cdot 3^{3}+2\cdot 3^{2}+1\cdot 3+0+2\cdot 3^{-1}+% 1\cdot 3^{-2}$ $\displaystyle=$ $\displaystyle 1\cdot 81+2\cdot 9+1\cdot 3+\frac{2}{3}+\frac{1}{9}$ $\displaystyle=$ $\displaystyle 102.777777\dots$

Note that there is no exact decimal representation of the ternary value 10210.21. This happens often with conversions between bases. This is why many decimal values (such as $0.1$) cannot be represented precisely in binary floating point (generally used in computers for non-integral arithmetic).

3. 3.

Hexadecimal to Decimal Let $b=16$. Convert 4ad9.e3 to decimal.

 4ad9.e3 $\displaystyle=$ $\displaystyle 4\cdot 16^{3}+10\cdot 16^{2}+13\cdot 16+9+14\cdot 16^{-1}+3\cdot 1% 6^{-2}$ $\displaystyle=$ $\displaystyle 4\cdot 4096+10\cdot 256+13\cdot 16+9+\frac{14}{16}+\frac{3}{256}$ $\displaystyle=$ $\displaystyle 16384+2560+208+9+\frac{227}{256}$ $\displaystyle=$ $\displaystyle 19161.88671875$

Iterative Method

Obviously base conversion can become very tedious. It would make sense to write a computer program to perform these operations. What follows is a simple algorithm that iterates through the symbols of a representation, accumulating a value as it goes along. Once all the symbols are iterated, the result is in the accumulator. This method only works for whole numbers. The fractional part of a number could be converted in the same manner, but it would have to be a separate process, working from the end of the number rather than the beginning.

Algorithm Parse($(A,n,b)$)
Input: Sequence $A$, where $0\leq A[i] for $1\leq i\leq n$, $b>1$
Output: The value represented by $A$ in base $b$ (where $A[1]$ is the left-most digit)
$v\leftarrow 0$
for $i\leftarrow 1$ to $n$ do
$v\leftarrow b*v+A[i]$

Parse$\leftarrow v$

This is a simple enough method that it could even be done in one’s head.

Example of Iterative Method

Let $b=2$. Convert 101101 to decimal.

Remaining Digits Accumulator
101101 0
01101 $0*2+1=1$
1101 $1*2+0=2$
101 $2*2+1=5$
01 $5*2+1=11$
1 $11*2+0=22$
$22*2+1=45$

This is a bit easier than remembering that the first digit corresponds to the $2^{5}=32$ place.

Conversion From Base 10

To convert a value to some base $b$ simply consists of inverse application of the iterative method. As the iterative method for “parsing” a symbolic representation of a value consists of multiplication and addition, the iterative method for forming a symbolic representation of a value will consist of division and subtraction.

Algorithm Generate($(A,n,b)$)
Input: Array $A$ of sufficient size to store the representation, $n>0,b>1$
Output: The representation of $n$ in base $b$ will be stored in array $A$ in reverse order
$i\leftarrow 1$
while $n>0$ do
$A[i]\leftarrow n$ mod $b$ (remainder of $n/b$)

$n\leftarrow n/b$ (integral division)

$i\leftarrow i+1$

Example of Conversion from Base 10

Convert the decimal value 20000 to hexadecimal.

$n$ Value Sequence Symbolic Sequence
20000 $\left\{\right\}$
1250 $\left\{0\right\}$ 0
78 $\left\{2,0\right\}$ 20
4 $\left\{14,2,0\right\}$ e20
0 $\left\{4,14,2,0\right\}$ 4e20

Note how we obtain the symbols from the right. We get the next symbol (moving left) by taking the value of $n$ mod $16$. We then replace $n$ with the whole part of $n/16$, and repeat until $n=0$.

This isn’t as easy to do in one’s head as the other direction, though for small bases (e.g. $b=2$) it is feasible. For example, to convert 20000 to binary:

$n$ Representation
20000
10000 0
5000 00
2500 000
1250 0000
625 0 0000
312 10 0000
156 010 0000
78 0010 0000
39 0 0010 0000
19 10 0010 0000
9 110 0010 0000
4 1110 0010 0000
2 0 1110 0010 0000
1 00 1110 0010 0000
0 100 1110 0010 0000

Of course, remembering that many digits (15) might be difficult.

Conversion Between Similar Bases

The digits in the previous example are grouped into sets of four both to ease readability, and to highlight the relationship between binary and hexadecimal. Since $2^{4}=16$, each group of four binary digits is the representation of a hexadecimal digit in the same position of the sequence.

It is trivial to get the octal and hexadecimal representations of any number once one has the binary representation. For instance, since $2^{3}=8$, the octal representation can be obtained by grouping the binary digits into groups of 3, and converting each group to an octal digit.

 $100\>111\>000\>100\>000=47040$

Even base $2^{5}=32$ could be obtained:

 $10011\>10001\>00000=jh0$
Title base conversion BaseConversion 2013-03-22 13:03:50 2013-03-22 13:03:50 mathcam (2727) mathcam (2727) 12 mathcam (2727) Application msc 11B13