0

taking in account that FFFFFFB2 in HEX is -78 in decimal

Why I have an error with this operacion ?

Integer.parseInt("FFFFFFB2", 16)

Error:

Exception in thread "main" java.lang.NumberFormatException: For input string: "FFFFFFFFFFFFFFB2"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:495)
    at tmp.Test.main(Test.java:11)
Nuñito Calzada
  • 4,394
  • 47
  • 174
  • 301
  • 1
    Did you know that the number you are trying to convert is a huge number and is equivalent to 18446744073709552000 in decimal? – SMA Jan 23 '16 at 14:22

2 Answers2

1

taking in account that FFFFFFFFFFFFFFB2 in HEX is -78 in decimal"

Nope, FFFFFFFFFFFFFFB2 hex is 18446744073709552000 decimal. You're mistaking hex for 2s complement.

If you want to take a 2s complement bit pattern and convert it into a number, this answer suggests a trick:

long l = new BigInteger("FFFFFFFFFFFFFFB2",16).longValue();

And it actually works.

Note I used BigInteger and long, rather than long and int, as the number is too big.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

According to the documentation:

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

The value represented by the string is not a value of type int.

As already pointed out, your number is not an int.

Community
  • 1
  • 1
Baderous
  • 1,069
  • 1
  • 11
  • 32