DJ_AM_Juicebox
asked on
Parsing a hex string into an integer?
Hi,
What's the correct way to parse a hex integer string?:
String str = "0xFF222222";
int val = Integer.parseInt(str, 16);
The above throws a number format exception.
Thanks
What's the correct way to parse a hex integer string?:
String str = "0xFF222222";
int val = Integer.parseInt(str, 16);
The above throws a number format exception.
Thanks
You should remove 0x part
String hex = "FF22222";
long num = Long.parseLong(hex,16);
System.out.println("num: " + num);
output:
num: 267526690
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
When parsing a hex string you need to remove the 0x from the string as for_yan has mentioned above.
The 0x is not part of the actual hex value you are parsing.
The 0x is not part of the actual hex value you are parsing.
Perhaps thsi could help
http://www.roseindia.net/java/java-conversion/HexadecimalToBinaryAndLong.shtml