Base Conversion

This program can be tricky for some folks. Don't be afraid to ask questions.

For this problem, nb, is the representation of an integer value, n, in base b. For example, 2110 = 258. One algorithm for converting a base 10 number to another base b involves a series of division operations, in which the divisor is always b. Each time a division is performed the remainder and quotient are saved. The quotient from each step becomes the dividend in the next step; the divisor is always b. The algorithm stops when the quotient is 0. The represenation of the number in the new base is the sequence of the remainders in reverse order (the last remainder computed is the leftmost digit--the most significant digit; the first one computed is rightmost--the least significant digit).

Example--determine the base 8 representation of 13910:

139/8 = 17 (the quotient) with a remainder of 3

17/8 = 2 (the quotient) with a remainder of 1

2/8 = 0 (the quotient) with a remainder of 2

Therefore, 13910 is 2138.

Here's a related base 10 example. Suppose you have an integer variable x in a Java program. You don't know in advance what the value of the variable is (perhaps the user has input it), but you'd like to know what the least significant digit will be when x when it is expressed as a base-10 value. I.e., you'd like to know what the least significant digit is of x10

x%10 gives you the answer. (The remainder of dividing x by 10.) As an example, if x is 123, then dividing x by 10 yields 12, with a remainder of 3. Note that 3 is the rightmost (i.e., least significant) digit of 12310. Note that because both x and 10 are integers, Java treats the "/" as integer division. I.e., it "drops" any fractional part of the division. Using our example value of x=123 again, x/10 yields 12, while x%10 yields 3.

Note that having obtained the 12 and hopefully stored it in another variable (or even back into x). You could now repeat our process to obtain the next most significant digit of the base ten representation of x, which is 2. You could repeat the process yet again to obtain the next most significant digit of the representation, which is 1. It only remains to print the 3, the 2 and the 1 in the correct order to get the result of converting the original value of x (12310) into the string "123". Here's a recap of the process:

123%10 = 3 <=== least significant digit of the base 10 number we are generating

123/10 = 12 <=== the quotient is 12, and this will be the dividend of the next step....

12%10 = 2 <=== middle digit of the base 10 number we are generating

12/10 = 1 <=== the quotient is 1, and this will be the dividend of the next step....

1%10 = 1 <=== most significant digit of the base 10 number we are generating

1/10 = 0, so we're done.

The same process will work for converting x into whatever base we want. For example, if I wanted to convert it into base 8 notation (called "octal") I would repeatedly calculate the remainder of division by 8 to access the individual digits of the octal representation. I would use and integer division by 8 to get successive quotient so as to be able to access the more significant digits. As an example, let's look at how we can calculate that 12310 is 1738:

123%8 = 3 <=== least significant digit of the base 8 representation of x

123/8 = 15

15%8 = 7 <=== middle digit of the base 8 representation of x

15/8 = 1

1%8 = 1 <=== most significant digit of the base 8 representation of x

1/8 = 0, so we're done.

 

In this exercise you will use this algorithm to write a program that converts a base 10 number to a 4-digit number in another base (we haven't covered enough in class yet to allow you to be able to convert if you don't know how many digits there will be). The base 10 number and the new base (between 2 and 9) will be input to the program. The start of the program is in the file BaseConvert.java. Save this file to your directory, then modify it one step at a time as follows:

  1. The program will only work correctly for base 10 numbers that can be represented by 4 digits in the new base. We know that in base 2 the maximum unsigned integer that will fit in 4 bits is 11112 which equals 1510 (or 24 - 1). In base 8, the maximum number is 77778 which equals 409510 (or 84 - 1). In general, the maximum base 10 number that fits in 4 base b digits is b4 - 1.
    1. Add an assignment statement to the program to compute this value for the base that is input (i.e., the value in base) and assign it to the variable maxNumber.
    2. Add a statement that prints out this result (appropriately labeled). Compile and run the program to make sure the program is correctly calculating the largest value that can be represented by three different values of base.
  2. Now add the code to do the conversion. The comments below guide you through the calculations -- replace them with the appropriate Java statements.
        // First compute place0 -- the units place.  Remember this comes
        // from the first division so it is the remainder when the
        // base 10 number is divided by the base (HINT %).
        // Then compute the quotient (integer division / will do it!) -
        // You can either store the result back in base10Num or declare a
        // new variable for the quotient
    
    
        // Now compute place1 -- this is the remainder when the quotient
        // from the preceding step is divided by the base.  
        // Then compute the new quotient
    
    
        // Repeat the idea from above to compute place2 and the next quotient
    
    
        // Repeat again to compute place3
    
    
  3. So far the program does not print the answer. Recall that the answer is the sequence of remainders written in reverse order -- note that this requires concatenating the four digits that have been computed (they should be in place0, place1, place2, place3). Since they are each integers, if we just add them the computer will perform arithmetic instead of concatenation. Here's a trick for converting numbers to strings: just "add" the number to a String.
    Example:
    String bob;  //Initialize bob to be empty string
    int x = 567;
    bob = "" + x;  // bob is now the String "567".
    bob = bob + 81;  // bob is now the String "56781".
    So, we will use a variable of type String. Near the top of the program a variable named baseBNum has been declared as an object of type String and initialized to an empty string. Add statements to the program to concatenate the digits in the new base to baseBNum and then print the answer. Compile and run your program. Test it using the following values: Enter 2 for the base and 13 for the base 10 number -- the program should print 1101 as the base 2 value; enter 8 for the base and 1878 for the number -- the program should print 3526 for the base 8 value; enter 3 for the base and 50 for the number -- the program should print 1212.
  4. Submit your program, along with a text document showing the output generated by your program for 3 different inputs (not those of the sample output, below).

Here is sample output from a solution to this assignment:

Base Conversion Program

Please enter a base (2 - 9): 5
The largest base 10 value we can represent with four digits in base 5 is 624
Please enter a base 10 number between 0 and 624 to convert to base 5: 123
The base 5 representation of 123 (base 10) is 0443 (base 5)

Base Conversion Program

Please enter a base (2 - 9): 7
The largest base 10 value we can represent with four digits in base 7 is 2400
Please enter a base 10 number between 0 and 2400 to convert to base 7: 2209
The base 7 representation of 2209 (base 10) is 6304 (base 7)

Base Conversion Program

Please enter a base (2 - 9): 3
The largest base 10 value we can represent with four digits in base 3 is 80
Please enter a base 10 number between 0 and 80 to convert to base 3: 65
The base 3 representation of 65 (base 10) is 2102 (base 3)