1

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

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

I think it might be because it is a minus value

Any ideas why or how or how to fix it?

EDIT:

Sorry, didn't include the actual error. here it is:

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)

EDIT 2:

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

Integer.toHexString(int_val);

EDIT 3: Turns out it is a known bug (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4215269) Although you can convert integers to hex strings, you cannot convert them back if they are negative numbers!!

user1270235
  • 359
  • 3
  • 4
  • 11

2 Answers2

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.

juergen d
  • 201,996
  • 37
  • 293
  • 362
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

Umesh Aawte
  • 4,590
  • 7
  • 41
  • 51