aspose file tools
The moose likes Beginning Java and the fly likes Hex to long conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Hex to long conversion" Watch "Hex to long conversion" New topic
Author

Hex to long conversion

Anuj Joshi
Greenhorn

Joined: Feb 05, 2009
Posts: 22
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
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

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).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32599
    
    4
. . . and welcome to JavaRanch
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Hex to long conversion
 
Similar Threads
Non printable characters
Difference between the two types of tostring methods?
maintain CR/LF characters in output file
How do I convert a big-endian(64 bit) to little endian(64 bit) in Java
Does it correct the way I catch the error?