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 . This number is given as a sequence of symbols . This sequence represents
This is straight-forward enough. All we need to do is convert each symbol to its decimal equivalent. Typically this is simple. The symbols usually represent the same value in any other base. For , the letters of the alphabet begin to be used, with and so on. This serves up to . Since the most common bases used are binary (), octal (), decimal (), and hexadecimal (), 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.
Binary to Decimal Let . Convert
10010.011
to decimal.10010.011 -
2.
Ternary to Decimal Let . Convert
10210.21
to decimal.10210.21 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 ) cannot be represented precisely in binary floating point (generally used in computers for non-integral arithmetic). -
3.
Hexadecimal to Decimal Let . Convert
4ad9.e3
to decimal.4ad9.e3
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()
Input: Sequence , where for ,
Output: The value represented by in base (where is the left-most digit)
for to do
Parse
This is a simple enough method that it could even be done in one’s head.
Example of Iterative Method
Let . Convert 101101
to decimal.
Remaining Digits | Accumulator |
---|---|
101101 | 0 |
01101 | |
1101 | |
101 | |
01 | |
1 | |
This is a bit easier than remembering that the first digit corresponds to the place.
Conversion From Base 10
To convert a value to some base 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()
Input: Array of sufficient size to store the representation,
Output: The representation of in base will be stored in array in reverse order
while do
mod (remainder of )
(integral division)
Example of Conversion from Base 10
Convert the decimal value 20000 to hexadecimal.
Value Sequence | Symbolic Sequence | |
---|---|---|
20000 | ||
1250 | 0 | |
78 | 20 | |
4 | e20 | |
0 | 4e20 |
Note how we obtain the symbols from the right. We get the next symbol (moving left) by taking the value of mod . We then replace with the whole part of , and repeat until .
This isn’t as easy to do in one’s head as the other direction, though for small bases (e.g. ) it is feasible. For example, to convert 20000 to binary:
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 , 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 , the octal representation can be obtained by grouping the binary digits into groups of 3, and converting each group to an octal digit.
Even base could be obtained:
Title | base conversion |
---|---|
Canonical name | BaseConversion |
Date of creation | 2013-03-22 13:03:50 |
Last modified on | 2013-03-22 13:03:50 |
Owner | mathcam (2727) |
Last modified by | mathcam (2727) |
Numerical id | 12 |
Author | mathcam (2727) |
Entry type | Application |
Classification | msc 11B13 |