Base Converter
|
A German merchant of the fifteenth century asked an eminent professor
where he should send his son for a good business education. The professor
responded that German universities would be sufficient to teach the boy
addition and subtraction but he would have to go to Italy to learn
multiplication and division. Before you smile indulgently, try multiplying
or even just adding the Roman numerals CCLXIV, MDCCCIX, DCL, and MLXXXI
without first translating them.
John Allen Paulos, Beyond Numeracy
|
I began planning this page as an introduction into binary systems. As the first step,
I applied JavaScript to create a converter between the binary and decimal systems. But what
do you know? It was easier and more natural to write a generic base converter than doing
anything special for the binary system. JavaScript is really fantastic. I am looking forward
to trying Java as well. Then this is what came out of my first attempt to create a binary
converter. It is a more or less general converter between representations of the same number
in various base system. Please note that the number of digits in any base (radix) N
is exactly the same number N. For example, in the binary (N = 2) system there are two digits: 0 and 1;
in the decimal (N = 10) there are ten of them: 0,1,2,3,4,5,6,7,8,9. What if N exceeds 10?
If N > 10, the missing digits come from the alphabet (usually disregarding the case.) Thus
A stands for the decimal 10 in any number system with base greater than 10. B stands for the decimal 11 in any number system with base greater than 11, and so on.
Returning to the converter, for now, you can enter a number in any system. Then just click anywhere
outside the edit control box used to input the number. Its representation in
other systems will appear in the corresponding text boxes.
It is customary to prefix hexadecimal numbers with 0x and octals with 0. The converter will accept this common notation which is, however, not necessary.
Please note the following. Representation of a number in a system with base (radix) N may only consist of digits that are less than N.
More accurately, if
| (1) |
M = akNk + ak-1Nk-1 + ... + a1N1 + a0
|
with 0 ≤ ai < N we have a representation of M in base N system and write
If we rewrite (1) as
| (2) |
M = a0 + N·(a1 + N·(a2 + N·...))
|
the algorithm for obtaining coefficients ai becomes more obvious. For example, a0 = M (mod N) and a1 = (M/N) (mod N), and so on. (K. Atkinson addresses specifics of the conversion between binary, decimal and hexadecimal systems in his Elementary Numerical Analysis, John Wiley & Sons, 1985.) Elsewhere I explain how to implement this procedure in both recursive and iterative manners.
Here, at one stage of conversion I use a built-in function parseInt which does not seem to return whenever this condition is violated by the very first digit. This appears to be a bug in the parseInt function. I am looking into this matter. For now, please follow the rule:
Representation of a number in a system with base (radix) N may only consist of digits that are less than N.
(The converter works this way: first type a number in the base you wish to convert from. Then click in any other input box.)
Would you like to have conversions in additional bases?
The books below, as most others, describe how to convert between various systems but seldom
address the question of arithmetic operations in different bases. ([Atkinson] demonstrates how
addition and multiplication work in the binary system.) The reason is it's all about the same.
Once you know how to do that in the decimal system, you are supposed to know how to handle the
same thing in other bases. However, this logic has little appeal to most of us. So I have put together
a page devoted exclusively to the Arithmetic Operations in Various Bases.
References
- K. Atkinson, Elementary Numerical Analysis, John Wiley & Sons, 1985
- W. Dunham, The Mathematical Universe, John Wiley & Sons, 1994
- Oystein Ore, Number Theory and Its History, Dover Publications, 1976
- J. A. Paulos, Beyond Numeracy, Vintage Books, 1992
Copyright © 1996-2008 Alexander Bogomolny
|