• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sam Saha
Ranch Hand
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the largest hex number which will fit into an int?
 
Sam Saha
Ranch Hand
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"000016A100"
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you understand that the number 16A10000006800000000 is too large to fit in an int. You should use a long instead.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Saha wrote:"000016A100"
No it isn’t. It is 0x7fffffff.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More details of the limits in the Java Language Specification. You have to scroll down a bit to find it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic