String d="0xD437D437";
System.out.println(Integer.decode(d));
CodePudding user response:
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
CodePudding user response:
The docs state
This sequence of characters must represent a positive value or a NumberFormatException will be thrown.
But 0xD437D437
is a negative value, namely -734538697
, as can be seen by Integer.toHexString(-734538697)
resulting in "d437d437"
.
Therefore either parse it as a Long
as @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 resulting Long
back into an Integer
/ int
:
Long.decode("0xD437D437").intValue() // -734538697