String d="0xD437D437";
System.out.println(Integer.decode(d));
Exception in thread "main" java.lang.NumberFormatException: For input string: "D437D437" under radix 16
276 Views Asked by sathish At
2
There are 2 best solutions below
0
On
Your number is too big to fit in integer type - 0x7FFFFFFF in hex is the largest number (2147483647 = 2^31-1). If you want to use this String as a number, use Long.decode(d) to get it as Long.
String d = "0x7FFFFFFF";
String e = "0x80000000";
String f = "0xD437D437";
System.out.println(Integer.decode(d));
System.out.println(Long.decode(e));
System.out.println(Long.decode(f));
will print
2147483647
2147483648
3560428599
The docs state
But
0xD437D437is a negative value, namely-734538697, as can be seen byInteger.toHexString(-734538697)resulting in"d437d437".Therefore either parse it as a
Longas @Bonniu says if you are looking for a positive value or if you are actually trying to parse a negative int you could just convert the resultingLongback into anInteger/int: