Results 1 to 5 of 5
  1. #1
    jnomani is offline Member
    Join Date
    Jun 2012
    Rep Power

    Question 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);
    			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;
    				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. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Rep Power

    Default Re: Help With Number Conversion

    Java Code:

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power

    Default 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.

    [Edit] 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. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power

    Default 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;
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jnomani is offline Member
    Join Date
    Jun 2012
    Rep Power

    Default 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.

Similar Threads

  1. Replies: 8
    Last Post: 01-21-2012, 02:14 AM
  2. Dice help. posting the number of times a number is rolled.
    By cookiejarvus in forum New To Java
    Replies: 13
    Last Post: 12-05-2011, 12:08 AM
  3. Replies: 1
    Last Post: 10-28-2011, 08:18 AM
  4. Any Other Methods for Number to Word Conversion?
    By arnelcolar in forum New To Java
    Replies: 14
    Last Post: 04-14-2011, 04:45 PM
  5. Printing the Number of Times a Number in a Range Shows up
    By space4rent00 in forum New To Java
    Replies: 1
    Last Post: 02-05-2010, 11:42 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts