Created
December 7, 2011 02:20
-
-
Save khotyn/1441135 to your computer and use it in GitHub Desktop.
Signed hex String to int.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Given a hex string that represent a negative integer, say
ffeb792e
,Integer.valueOf(String str, int radix)
will throw aNumberFormatException
, this piece of code could fix this problem.