| Author |
Converting char array to int
|
Roger F. Gay
Ranch Hand
Joined: Feb 16, 2007
Posts: 349
|
|
I've been staring at this for hours, trying this and that, and still don't see what's wrong. I know that it's ok to:
But in the context of this code, I get a NumberFormatException. As you can see, I've been looking at the character array and String and don't see any problem with it. When I simplify (beyond what is possible in the actual application) by just setting a string value to the number that I get from the algorithm, all is fine. When I create a char[] array and set it, all is fine. Just can't see why this is throwing the exception.
|
Correlation does not prove causality.
|
 |
Ogeh Ikem
Ranch Hand
Joined: May 13, 2002
Posts: 180
|
|
|
The number (3884252680) looks too big for a Java Integer.
|
 |
Unnar Björnsson
Ranch Hand
Joined: Apr 30, 2005
Posts: 164
|
|
|
That's because you are trying to convert the string "3884252680" to an int where the largest value int can hold is 2147483647
|
 |
Roger F. Gay
Ranch Hand
Joined: Feb 16, 2007
Posts: 349
|
|
DO-OH! I knew it had to be something basic. All I had in mind was that the number had to hold 10 decimal places or less. Thanks.
What can I do to fix it? I don't think there's a BigInteger.parseBigInt().
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
|
Long.parseLong?
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Roger F. Gay
Ranch Hand
Joined: Feb 16, 2007
Posts: 349
|
|
I fixed the posted problem by using long instead of int. But I'm concerned (and don't know a big-endian from a little indian). The test process came from a real example. From the spec:
For each of these fields, the server has to take the digits from the
value to obtain a number (in this case 1868545188 and 1733470270
respectively), then divide that number by the number of spaces
characters in the value (in this case 12 and 10) to obtain a 32-bit
number (155712099 and 173347027). ,,,,,
.....
The concatenation of the number obtained from processing the |Key1| field,
expressed as a big-endian 32-bit number, the
number obtained from processing the |Key2| field, again
expressed as a big-endian 32-bit number, and finally the eight bytes
at the end of the handshake, form a 128 bit string .....
|
 |
Roger F. Gay
Ranch Hand
Joined: Feb 16, 2007
Posts: 349
|
|
|
Never mind, I guess. After dividing by the number of spaces, the result is 32-bit. No problem there. I just panicked because I don't really understand what a big-endian is and whether I have to do anything special.
|
 |
Unnar Björnsson
Ranch Hand
Joined: Apr 30, 2005
Posts: 164
|
|
Roger F. Gay wrote:Never mind, I guess. After dividing by the number of spaces, the result is 32-bit. No problem there. I just panicked because I don't really understand what a big-endian is and whether I have to do anything special.
Big endian logic stores the most significant byte (MSB) first. An int number is 32bit which means it takes 4 bytes in memory, so the number 123456789 (75BCD15 in hex) would be stored as [07] [5B] [CD] [15] where little-endian would store it as [15] [CD] [5B] [07]
|
 |
 |
|
|
subject: Converting char array to int
|
|
|