Practice exercise -- Base Conversion
1. Develop a script to convert a decimal number into its binary equivalent.
Allow the script user to enter the decimal number [and the target base].
Assign the decimal number to a variable for the dividend. The conversion
may be done by repeated division of the decimal number by the base while
the quotient computed has not become zero. That is, divide this number by
the base(e.g. 2) with the quotient assigned to one variable and the remainder
to another. The remainder digit becomes the next digit, to the left, in
the answer. Replace the decimal number (i.e. dividend) with the quotient.
Note that for decimal-to-hexadecimal conversion the remainder will be in
the range 0 to 15. For remainders greater than 9, assign to the answer
variable the corresponding hex digit (i.e. 'A' for a remainder of 10 ...).
2. Use positional notation to convert a binary or a hexadecimal value to a
decimal value. For the base-R number system, a number with n digits,
where di represents the ith position of the number, this may be
accomplished by evaluating the polynomial:
dn * Rn-1 + dn-1 * Rn-2 + ... d2 * R + d1
for example: 101012
24 23 22 21 20
1 0 1 0 1 --> 1 * 16 + 0 * 8 + 1 * 4 + 0 * 2 + 1 * 1 = 2110
To implement this technique as a program, create a repetition structure to
sum the products of the digits times the base raised to the power denoted by
each position. Note that unless string or array objects are created, consider
prompting the user for the number of digits in the number to be converted and
have a "loop" to take one digit at a time. The "loop" would contain a prompt
and input for the next digit(from the right), compute the product of that
digit raised to the power represented by that digits position(from the right),
and add that product to an accumulator variable. Also note that before
computing each product, a hex digit may need to be converted to decimal.