Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting char array to int

 
Roger F. Gay
Ranch Hand
Posts: 408
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The number (3884252680) looks too big for a Java Integer.
 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 408
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Long.parseLong?
 
Roger F. Gay
Ranch Hand
Posts: 408
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 408
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 164
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic