Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • 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
  • 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
Posts: 48363
56
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"000016A100"
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • 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
Posts: 48363
56
  • 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
Posts: 48363
56
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic