• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

is this a bug with Long.parseLong() code?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am really bogged down on this. I am parsing hex strings to long values. I use the call
Long.parseLong(string, 16)
it processes quite a few of the strings just fine then throws this:
java.lang.NumberFormatException: For input string: "a6acda419199c58a"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Long.parseLong(Long.java:406)
it looks like hex to me? am I missing something?
It's compiled and run with JDK 1.4.2.
Any comments are really welcome.
Cheers
Rich.
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"a" is greater than 7, so the most significant bit/sign bit is set. This makes your number greater than Long.MAX_LONG. If you sent the corrisponding negative number it should parse without complaining. One way to get around this issue is to call new BigInteger(value, 16).longValue().
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rich,
I think the number is just too big for a long variable. Using a calculator this hex converts to 12010214281440773514 in decimal. If you try...

this happens...
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
There's nothing wrong with your String.
The only problem is that the long value derived from it is too
big - and that's why you get the exception.
A long value can be very large, but not large enough for your
hex.
Nimo.
 
Richard Johnson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the help. I didn't think it would be too big because the hex 'should' have originally been generated from a long value. Strange... my problem must lie elsewhere in the code ( Okay thanks guys for the quick assistance.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic