base_conversions
Tutorial: Converting From One Number Base To Another S. Gever
----------------------------------------------------
When you count in some numbering system, the counting sequence is represented
by a given set of symbols, that are arranged according to a set of rules. For
instance, in the decimal numbering system, the set of symbols is the familiar
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }. The rules are that the counting sequence
starts at zero, represented by the first symbol in the set, and the
representation of a sequential count proceeds through this set of symbols,
in order, for its representation.
Example:
count | zero|one|two|three|four|five|six|seven|eight|nine
symbol | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
The next rule is when a count exceeds the value representable by any of the
allowed symbols, then use a combination of allowed symbols whose grouping
tracks multiples of the numbering system base raised to a power equal to
the symbols position in the sequence of symbols.
Example: Five hundred twenty-eight is written as ==> 528
The base is 10 and 528 is understood to be shorthand
notation for:
5 * 10^2 + 2 * 10^1 + 8 * 10^0 note: ^ means raise to
= 5 * 100 + 2 * 10 + 8 * 1 the power
= 500 + 20 + 8
= 528
In general, some number A, written in base b, can be expressed as
A = a *b^n + a *b^(n-1) + ... + a b^1 + a b^0
n n-1 1 0
where n is the position of a digit. The low order digit is the one
on the far right of a number and it's position n is 0. The positions
are counted from the right, to the left.
I. Converting a number in base b to decimal
----------------------------------------
Example: Convert the base 8 (octal) number 177, to decimal.
The set of symbols for the octal numbering system is
{ 0, 1, 2, 3, 4, 5, 6, 7 }.
The base is 8 and 177 is understood to be shorthand
notation fo:
1 * 8^2 + 7 * 8^1 + 7 * 8^0
= 1 * 64 + 7 * 8 + 7 * 1
= 64 + 56 + 7
= 127
Example: Convert the base 16 (hexadecimal) number 4af, to decimal.
The set of symbols for the hexadecimal numbering system is
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f }.
The base is 16 and 4af is understood to be shorthand
notation for:
4 * 16^2 + a * 16^1 + f * 16^0
= 4 * 256 + a * 16 + f * 1
= 4 * 256 + 10 * 16 + 15 * 1
The last step illustrates a tactic for dealing with the
fact that I can not think arithmetically in hexadecimal. I
had to change the hexadecimal symbols to their equivalent
decimal symbols so I could calculate the product terms. In
other words. I understood the symbology as hexidecimal but
my computational base is decimal.
= 1024 + 160 + 15
= 1199
II. Converting a decimal number to a number in base b
-------------------------------------------------
Example: Convert the decimal number 235 to a base 6 number
The set of symbols for the base 6 numbering system is
{ 0, 1, 2, 3, 4, 5, }.
The base is 6.
39 6 1 0
------ ----- ---- ----
6 ) 235 6 ) 39 6 ) 6 6 ) 1
18 36 6 0
----- ---- --- ---
55 3 0 1
54
-----
1
The base 6 number is 1031 which is the sequence of
remainders in reverse order of production.
Example: Convert the decimal number 1199 to a base 16 (hexadecimal) number
The set of symbols for the hexadecimal numbering system is
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f }.
The base is 16.
74 <- This quotient is the dividend for the next
------- division. It represents an as of yet
16 ) 1199 undetermined number of groups of sixteen.
112 Regardless of how many, it has left us with a
------ group less than sixteen, called the remainder.
79
64
------
15 <- This remainder is the low order digit of the
hexadecimal number. 15 -> f
4
-------
16 ) 74
64
------
10 <- This remainder is the next higher order digit of
the hexadecimal number. 10 -> a
0
-------
16 ) 4
0
------
4 <- This remainder is the high order digit of the
hexadecimal number. 4 -> 4
The base 16 number is 4af which is the sequence of
remainders in reverse order of production.
All the steps illustrate again the tactic of converting the
unfamiliar symbology of the non-decimal base to the
computational base 10. The divisor, if unconverted, would
have appeared as follows:
7
-------
f ) 1199
112
------
...
III. Converting a number in base b to a number in base c: b,c <> 10
--------------------------------------------------------------
Example: Convert the base 20 number 3gi, to a base 16 number.
The set of symbols for the base 11 numbering system is
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f }.
The set of symbols for the base 20 numbering system is
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f, g, h, i, j }.
------
f ) 3gi <-This sure looks difficult! The divisor value can
be converted to a computational base(ie. 10), but
what do we do about the dividend. Well, it also
needs to be converted to a computational base(ie. 10)
Let's pick the low hanging fruit first. The divisor
in base 16 is equal to 15 in base 10. f==15. Now
let's convert the dividend
3 * 20^2 + g * 20^1 + i * 20^0
= 3 * 400 + g * 20 + i * 1
= 3 * 400 + 16 * 20 + 18 * 1
= 1200 + 320 + 18
(base 20) 3gi = 1538 (base 10)
96 6 0
------- ----- ----
16 ) 1538 16 ) 96 16 ) 6
144 96 0
----- --- ---
98 0 6
96
-----
2
The base 20 number is the remainders in reverse order:
6 0 2
which fortunately, needs no further conversion
Generated by GNU enscript 1.6.1.