Skip to content

Instantly share code, notes, and snippets.

@khotyn
Created December 7, 2011 02:20
Show Gist options
  • Save khotyn/1441135 to your computer and use it in GitHub Desktop.
Save khotyn/1441135 to your computer and use it in GitHub Desktop.
Signed hex String to int.
public static int hexStringToInt(String str) {
if (str.length() == 8) {
int firstBit = Integer.valueOf(str.substring(0, 1), 16);
if (firstBit >= 8) {
return Integer.valueOf((firstBit - 8) + str.substring(1), 16) - 0x80000000;
} else {
return Integer.valueOf(str, 16);
}
} else {
return Integer.valueOf(str, 16);
}
}
@khotyn
Copy link
Author

khotyn commented Dec 7, 2011

Given a hex string that represent a negative integer, say ffeb792e, Integer.valueOf(String str, int radix) will throw a NumberFormatException, this piece of code could fix this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment