Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NumberFormatException when converting a Hex String to Int

Tags:

java

I want to convert a Hex String to decimal, but I got an error in the following code:

String hexValue = "23e90b831b74";       
int i = Integer.parseInt(hexValue, 16);

The error:

Exception in thread "main" java.lang.NumberFormatException: For input string: "23e90b831b74"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:495)
like image 777
Nunyet de Can Calçada Avatar asked Dec 14 '15 09:12

Nunyet de Can Calçada


1 Answers

23e90b831b74 is too large to fit in an int.

You can easily see that by counting the digits. Each two digits in a hex number requires a single byte, so 12 digits require 6 bytes, while an int only has 4 bytes.

Use Long.parseLong.

String hexValue = "23e90b831b74";       
long l = Long.parseLong(hexValue, 16);
like image 120
Eran Avatar answered Nov 14 '22 22:11

Eran