This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Need Help for Converting a Hex String to an Integer in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Need Help for Converting a Hex String to an Integer in Java" Watch "Need Help for Converting a Hex String to an Integer in Java" New topic
Author

Need Help for Converting a Hex String to an Integer in Java

Sam Saha
Ranch Hand

Joined: Jan 23, 2010
Posts: 104
Hi

I am trying to convert a Hex String to Integer. But I am getting NumberFormatException when I run the code. I would appreciate if someone can help me so that I can convert the whole string into number.

My code:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37890
    
  22
What is the largest hex number which will fit into an int?
Sam Saha
Ranch Hand

Joined: Jan 23, 2010
Posts: 104
"000016A100"
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

So you understand that the number 16A10000006800000000 is too large to fit in an int. You should use a long instead.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37890
    
  22
Sam Saha wrote:"000016A100"
No it isn’t. It is 0x7fffffff.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37890
    
  22
More details of the limits in the Java Language Specification. You have to scroll down a bit to find it.
 
Don't get me started about those stupid light bulbs.
 
subject: Need Help for Converting a Hex String to an Integer in Java
 
Similar Threads
Converting to Hexadecimal literals
Conversion
converting float hex to int
Converting
Java Convert Hexadecimal number into Integer