# Base Conversion

• 12-10-2010, 03:01 PM
javaman1
Base Conversion
I am writing this program for my AP Java Programming class. The assignment is to enter a number 'n' and a number 'k'. The program must convert the number 'n' to base 'k' and then add the sum of the digits.

I have gone through this over and over and cannot figure out my logic error. For example, when I enter "255 base 2", the output is "65" when it should be 8. 255 is base 2 is 11111111, so the sum should be 1+1+1+1+1+1+1+1 = 8.

Can anyone help? I cannot figure out the logic error, and I have asked many of the other students for help (though less experienced in programming than me) and they cannot be of help either.

Code:

Code:

```//*************** //Adam Brewer  * //Advanced Lab 2* //*************** import java.util.Scanner; class SumOfBaseKDigits {         public static void main(String[] args)         {                 Scanner read = new Scanner(System.in);                 int base = 0;                 double testNum = 0;                 int exponent = 0;                 int sum = 0;                 int numInBase = 0;                 System.out.println("This program will convert a number to a certain base.");                 System.out.println("It will then add the converted digits.");                 System.out.println();                 System.out.print("Enter a test number: ");                 testNum = read.nextInt();                 System.out.print("Enter a base: ");                 base = read.nextInt();                 double[] numArray = new double[base+1]; //Initializes the array that will hold the expanded base numbers                 int[] sumsArray = new int[base+1]; //Initializes the array that will hold the digits in base "b"                 for(int e = 0; e < numArray.length; e++)                 {                         numArray[e] = Math.pow(base, exponent);                         exponent++;                 }                 for(int e = (numArray.length - 1); e >= 0; e--) //Converts the number to base "b"                 {                         if(testNum / numArray[e] >= 1)                         {                                 sumsArray[e] = (int)(testNum / numArray[e]);                                 testNum = testNum % numArray[e];                         }                 }                 for(int e = 0; e < sumsArray.length; e++) //Finds the sum of the digits in base "b"                 {                         sum += sumsArray[e];                 }                 System.out.println();                 System.out.println("The sum of the digits in base " + base + " is " + sum + ".");                 //Outputs the sum                 System.out.println();         } }```
• 12-10-2010, 05:39 PM
j2me64
i didn't analyze what's wrong with the code but you can find a good conversion example here or if you want to convert only bases of 2, 8 and 15 you can also use the methods of the Integer wrapper class, example

Code:

```                int i = 255;         // base 2         System.out.println(Integer.toBinaryString(i));         // base 8         System.out.println(Integer.toOctalString(i));         // base 15         System.out.println(Integer.toHexString(i));```
• 12-10-2010, 07:17 PM
javaman1
I see. But I need to convert to any input base; not just 2, 8, 15. Can you find the logic error?
• 12-10-2010, 07:40 PM
JosAH
Quote:

Originally Posted by j2me64
Code:

```                int i = 255;         // base 15         System.out.println(Integer.toHexString(i));```

Erm, sixteen, that's base sixteen, not base fifteen.

kind regards,

Jos ;-)
• 12-10-2010, 07:49 PM
javaman1
Oh, right. JoaSH, do you see the logic error?
• 12-10-2010, 07:59 PM
JosAH
Quote:

Originally Posted by javaman1
Oh, right. JoaSH, do you see the logic error?

Yes, his comment was incorrect.

kind regards,

Jos
• 12-10-2010, 08:32 PM
pbrockway2
Quote:

do you see the logic error?

Perhaps you could explain the algorithm you are using. Or say what output you get for some input, and what you expected.

-------------------------

It would be a good plan to use a method to do the conversion (and return an array of ints or whatever). That way the logic can be cleanly separated from the prompting and other input and output. So for this problem I would begin by implementing a simple method (of a few lines probably):

Code:

```    /**     * Returns the elements making up a given  number in a given base.     */ private static int[] convert(int num, int base) {     // code here }```

(Note in terms of finding the digit sum this array can be in descending or ascending place values: whatever is convenient.)

If you are finding the digits of an integer value in some base, why do you declare testNum and numArray as doubles?

Use System.out.println() often to print out the values of the arrays and of the loop indices so you can get a feel for what is happening. In this regard remember:

Code:

`System.out.println(Arrays.toString(numArray));`

Why do you make numArray and sumsArray the length you do? If base is 2 this would seem a little confining. Since you are using nextInt() to obtain the number to be converted you should, with a little thought, be able to figure out the maximum number of digits there will ever be.