2

I am getting an error because of the following line of code:

int x = color(Integer.parseInt("ffffffde",16));

The value "ffffffde" is being created by the following code:

Integer.toHexString(int_val);

Error:

Exception in thread "Animation Thread" java.lang.NumberFormatException: For input string: "ffffffde"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)

I think it might be because it is a negative value.

Any ideas why or how or how to fix it?


Edit:

Turns out it is a known bug JDK-4215269 in versions of Java prior to 8.

Although you can convert integers to hex strings, you cannot convert them back if they are negative numbers!

1
  • 1
    An error? Wonder what that might be. Commented May 21, 2012 at 7:51

3 Answers 3

12

ffffffde is bigger than integer max value

Java int is 32 bit signed type ranges from –2,147,483,648 to 2,147,483,647.

ffffffde = 4,294,967,262 

Edit

You used Integer.toHexString(int_val) to turn a int into a hex string. From the doc of that method:

Returns a string representation of the integer argument as an unsigned integer in base 16.

But int is a signed type.

USE

int value = new BigInteger("ffffffde", 16).intValue();

to get it back as a negative value.

Sign up to request clarification or add additional context in comments.

3 Comments

Right 4294967262 is out of int range
But the value is being created by using "Integer.toHexString(int_val)"
+1 good answer, by the way I also knew the person you are talking here.
5

If you are getting error like this,

Exception in thread "main" java.lang.NumberFormatException: For input string: "ffffffde"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:461)
    at com.TestStatic.main(TestStatic.java:22)

Then there is problem with value you are passing that is ffffffde . This is not a valid hex value for parsing to int.

Please try this

int x = Integer.parseInt("ffffde",16);
        System.out.println(x);

It should work.

For hex values more than that you have to pars to Long

Long x = Long.parseLong("ffffffde",16);
        System.out.println(x);

And this also should work

Comments

0

Using the Integer.parseUnsignedInt method introduced in Java 8 instead of Integer.parseInt will result in the desired behavior.

int x = color(Integer.parseUnsignedInt("ffffffde", 16));

The issue is that Integer.toHexString returns an unsigned integer in base 16, but Integer.parseInt parses the value as a signed integer. Since ffffffde as a signed integer is greater than Integer.MAX_VALUE, the observed NumberFormatException is thrown.

The Integer.toHexString Javadocs indicates that parseUnsignedInt can be used to convert the String back to an int:

The value of the argument can be recovered from the returned string s by calling Integer.parseUnsignedInt(s, 16).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.