1. Member
Join Date
Jun 2012
Posts
2
Rep Power
0

Help With Number Conversion

Hey guys I'm new to the Java community and have only been learning for about a year (I take a CS course at my High School). I am working on a project that involves converting different bases (Ex base-36, base-34, Octal, Hex etc.) into Decimal (base-10). here is my code:

Java Code:
```private static int decimateString(String nonDecimal, int Base){
char temp;
int result = 0;
int[] Block = new int[nonDecimal.length()];
for(int i = 0; i < Block.length; i++){
temp = nonDecimal.charAt(i);
switch(temp){
case 'A' : Block[i] = 10; break;
case 'B' : Block[i] = 11; break;
case 'C' : Block[i] = 12; break;
case 'D' : Block[i] = 13; break;
case 'E' : Block[i] = 14; break;
case 'F' : Block[i] = 15; break;
case 'G' : Block[i] = 16; break;
case 'H' : Block[i] = 17; break;
case 'I' : Block[i] = 18; break;
case 'J' : Block[i] = 19; break;
case 'K' : Block[i] = 20; break;
case 'L' : Block[i] = 21; break;
case 'M' : Block[i] = 22; break;
case 'N' : Block[i] = 23; break;
case 'O' : Block[i] = 24; break;
case 'P' : Block[i] = 25; break;
case 'Q' : Block[i] = 26; break;
case 'R' : Block[i] = 27; break;
case 'S' : Block[i] = 28; break;
case 'T' : Block[i] = 29; break;
case 'U' : Block[i] = 30; break;
case 'V' : Block[i] = 31; break;
case 'W' : Block[i] = 32; break;
case 'X' : Block[i] = 33; break;
case 'Y' : Block[i] = 34; break;
case 'Z' : Block[i] = 35; break;
default:
Block[i] = Integer.parseInt(nonDecimal.substring(i, i+1));
}
} for(int i = 0, b = Block.length - 1; i < Block.length; i++, b--){
result += (Block[i] * Math.pow(Base, b));
}
return result;
}

public static void main(String[] Args){ // Tests the conversion
String mes = "WELCOME";
System.out.println(decimateString(mes, 33));
}```
The problem I noticed is that if the String passed to the method, decimateString, is greater than 7 Characters long, the conversion will not work and instead it will always return the decimal number 2147483647.

Another thing, is that if the String is 7 Characters long, and I try to convert from a base higher than BASE-24, I get the same thing. Can anyone explain to me why this might be happening, and what a way to avoid this nuisance would be? Thanks a lot in advance!

2. Re: Help With Number Conversion

Java Code:
`System.out.println(Integer.MAX_VALUE);`
Hmmmmm!

3. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14

Re: Help With Number Conversion

Your decimateString() is producing an int which, in Java, can only be a certain size. Evidently some multiple of 7 to the power of 24 is enough to "overflow". There is also a long data type which be larger. (Only result need be changed from int to long, the other int values don't grow too large.)

There is also the BigInteger data type which can grow indefinitely large (or until your computer runs out of memory!) There is a problem here however, as using BigInteger makes your exercise trivial.

 My post was very slow as I looked about on the internet for someone converting arbitrarily long integers. Found this, but no code. If you think about it, it's a bit problematic: suppose you were converting a *very* long base 7 numeral into base 10. The the "digits" at the left (start/big) end would continue to have an effect all the way to the right hand end.

I did see references to him who knows - Knuth - who has apparently developed fast ways of doing this. The rest of us would end up working with Strings and reinventing (badly) BigInteger.
Last edited by pbrockway2; 06-06-2012 at 02:44 AM.

4. Re: Help With Number Conversion

Since the letters A to Z are contiguous in value in ASCII, you could replace the large switch statement with an if statement to test the char's value and range and then use a single assignment statement with arithmetic to convert the char to an int:
int val = temp - 'A' + 10;

5. Member
Join Date
Jun 2012
Posts
2
Rep Power
0

Re: Help With Number Conversion

Hey guys thanks a lot for the quick replies! I will change the return of the method to a long which seems more feasible.

I also took Norm's advice and instead of using a switch, I just converted it to a character using ASCII
Last edited by jnomani; 06-06-2012 at 03:37 AM.