Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: 0 seems to be comming out as 48?

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    63
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default 0 seems to be comming out as 48?

    I have writen the following code but for some reason all the numbers seem to be offset by 48?

    String bdString;
    Vector<Integer> v = new Vector<Integer>();
     
    BigDecimal bd = new BigDecimal(Math.pow(2, 1000));
    bdString = bd.toString();
     
    for (int i = 0; i < bdString.length(); i++) {
    	v.add((Integer.valueOf(bdString.charAt(i)).intValue()));
    }

    When I check the vector all 0's have been entered in as 48, all 1's have been entered in as 49, 2's as 50 etc. I know I'm going to feel like a fool when somebody points it out but... What is going on?


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: 0 seems to be comming out as 48?

    Hi tarkal,

    a char is stored as an int, thus when using Integer.valueOf(), you are essentially requesting the value of another interger, in ASCII 48 is the integer number of the character '0'. So you could just subtract 48 from each number, or consider the following method:

    Character.getNumericValue()

    Chris

  3. The Following User Says Thank You to Freaky Chris For This Useful Post:

    tarkal (November 13th, 2011)

  4. #3
    Member
    Join Date
    Sep 2011
    Posts
    63
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: 0 seems to be comming out as 48?

    Ooooo I like the look of Character.getNumericValue().

    Thanks