• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Hex to long conversion  RSS feed

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all ,

I am having a hex string as "d41d8cd98f00b204e9800998ecf8427e" ..
i need to convert this hex string into long
I did something following

String s="d41d8cd98f00b204e9800998ecf8427e";
long strLong= Long.parseLong(s,16);
System.out.println(strLong);


But at the runtime i am getting following error :
java.lang.NumberFormatException: For input string: "d41d8cd98f00b204e9800998ecf8427e"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Long.parseLong(Long.java:415)

Thanks in advance
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code, in concept, is fine. But a long in Java is a 8 bytes signed (two's complement) value. So it's maximum value is 2^63 - 1, or 7FFF FFFF FFFF FFFF in hex. The number you are providing is a wee bit bigger that that. ;) If you run your code with a smaller hex value it will work fine.

To work with a number that big, you'll need to use the BigInteger class (or BigDecimal if you need a floating number).
 
Marshal
Posts: 64089
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to JavaRanch
 
He's giving us the slip! Quick! Grab this tiny ad!
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!