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!!
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.
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
Related
I'm trying to convert my binary string to decimal value but i'm getting error.
The binary string is of 32 bits so no case of overflow.
String s = "11111111111111111111111111111101";
System.out.print(s.length());
System.out.print(Integer.parseInt(s,2));
Exception is:-
Exception in thread "main" java.lang.NumberFormatException: For input string: "11111111111111111111111111111101"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:583)
Integer type is not big as you expect, instead, try to use BigInteger:
new BigInteger(s, 2)
Output
324294967293
The first bit is 1, so this number cannot be represented as a positive int. If you try, it will "overflow" and become negative.
If that is what you want, you can parse the string as an unsigned value:
System.out.print(Integer.parseUnsignedInt(s,2));
Output: -3
You can use long:
String s = "11111111111111111111111111111101";
System.out.print(s.length());
System.out.print(Long.parseLong(s,2));
I want to convert Integer.MAX_VALUE to binary and want the represenation to be of type int. I passed it to Integer.tobinarystring() and wraped that with Integer.parseint but i get numberformatexception.
Here is the code
System.out.println(
Integer.parseInt(
Integer.toBinaryString(Integer.MAX_VALUE)
)
);
Here is the exception
Exception in thread "main" java.lang.NumberFormatException: For input string: "1111111111111111111111111111111"
Integer.MAX_VALUE is 2,147,483,647
In binary this is:
1111111111111111111111111111111
If we treat that like an integer again, which is 1,111,111,111,111,111,111,111,111,111,111 you can probably see that it is much larger than the max value.
You probably want to look into BigInteger if you really need to deal with that as a int.
If you want to get the integer value of the binary string
1111111111111111111111111111111
you must use another signature of parseInt() that takes as 2nd parameter the radix, in this case of a binary string the radix is 2
String str = Integer.toBinaryString(Integer.MAX_VALUE);
int number = Integer.parseInt(str, 2);
System.out.println(number);
it will print:
2147483647
I'm trying to convert the system time to int with the following code:
String today = "" + System.currentTimeMillis();
int todayInt = Integer.parseInt(today);
But I'm getting the following error:
java.lang.NumberFormatException: For input string: "1538956627792"
Why is this number: "1538956627792" still throwing an error?
Number is too long to be parsed as int, You need to use Long to parse that big number,
long todayInt = Long.parseLong(today);
The size of int type is 32 bit, it ranges from -2,147,483,648 to 2,147,483,647.
1538956627792 exceeds the range, so the error caused.
you could change int to long to solve this problem, here is a detailed reference
I'm doing a project and i have a really simple hash function in java that SHOULD read each "data" (which is a generic type that is a String or Double type read by file) character and make a sum of their values that will be used as hashcode.
I thought that i could convert each character to Hexadecimal, and then "decode" or "parseInt" the obtained String, but it does not work and i do not understand why.
Here is my method:
public long HashFunction(T data){
String bytes = data.toString();
int value=0;
for (int i=0; i<bytes.length(); i++)
value = value + Integer.decode(Integer.toHexString( bytes.charAt(i) | 0x100000).substring(1));
return (value%1583)%(size);
//1583 prime number not near to the power of 2, size is the size of the array of my hashtable
}
And here is my error, 0038 should be an "8":
Exception in thread "main" java.lang.NumberFormatException: For input string: "0038"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.valueOf(Unknown Source)
at java.lang.Integer.decode(Unknown Source)
at dizionario_package.HashTable.HashFunction(HashTable.java:22)
at dizionario_package.HashTable.HashInsert(HashTable.java:29)
at dizionario_package.RecordReader.CreateHTFromFile(RecordReader.java:24)
at dizionario_package.proviamo.main(proviamo.java:8)
Also, i'm sure that the error is in this function, because if i use the java hashcode method, it works.
Thanks in advance.
You need to tell decode you're using hex. Prefix the string with 0x.
value = Integer.decode("0x"+"10038".substring(1));
I have a file with many hex numbers (for eg - 0X3B4 ). Im trying to parse this file as assign these numbers to integers, but dont seem to get Integer.parseInt to work.
int testint = Integer.parseInt("3B4",16); <- WORKS
int testint = Integer.parseInt("0X3B4",16);
gives error:
Exception in thread "main" java.lang.NumberFormatException: For input string: "0x3b4"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
What is the right way to assign the value 0XB4 to an int ?
Do I have to get rid of the 0X - its not unusual to represent hex nos this way...
You can do
int hex = Integer.decode("0x3b4");
You are right that parseInt and parseLong will not accept 0x or 0X