Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected NumberFormatException while parsing a hex string to an int value

Tags:

java

hex

I want to parse an String containing 8 hex-digits (4bytes) but i got an NumberFormatException. What is wrong here?

assertThat(Integer.parseInt("FFFF4C6A",16),is(0xFFFF4C6A));
like image 732
Chriss Avatar asked Nov 22 '25 03:11

Chriss


1 Answers

Your number represents a number greater than that assignable to an int. Try:

Long.parseLong("FFFF4C6A", 16);

which gives 4294921322.

From the doc:

An exception of type NumberFormatException is thrown if any of the following situations occurs:

  • The first argument is null or is a string of length zero.
  • The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.
  • Any character of the string is not a digit of the specified radix, …
  • The value represented by the string is not a value of type int.

and it's the 4th case that you're hitting.

like image 96
Brian Agnew Avatar answered Nov 24 '25 18:11

Brian Agnew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!