quick java help.

minus1972

Platinum Member
Oct 4, 2000
2,245
0
0
hey,

I've got a stupid little problem holding up a whole project. My class basically tries to mock the BigInteger class. It takes in a string and stores the value as an array of integers. My problem is that when I take the individual chars and try to convert them into an integer, the wrong number is stored. For example...1 = 49, 2 = 50, 3 = 51, etc.

Here's the relevant code:

charToInt = val.charAt(size); //charToInt is type char
input = (int) charToInt; //input is type int
num = input; // at this point the number is f'd

any ideas? I can't use parseInt because it's a char, not a string.

Thanks.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
You're storing the ASCII value of each char rather than the numeric value. (see ascii table)

Subtract 48 from each number and you'll get the values you want.