Java 十六進位制轉十進位制正負數
阿新 • • 發佈:2018-12-04
/**
* 十六進位制轉正負數
* (2個位元組的)
*/
public static double parseHex4(String num) {
if (num.length() != 4) {
throw new NumberFormatException("Wrong length: " + num.length() + ", must be 4.");
}
int ret = Integer.parseInt(num, 16);
ret = ((ret & 0x8000) > 0) ? (ret - 0x10000) : (ret);
return (double) ret;
}
/**
* 十六進位制轉負數
* (4個位元組的)
*/
public static double parseHex8(String num) {
if (num.length() != 8) {
throw new NumberFormatException("Wrong length: " + num.length() + ", must be 4.");
}
BigInteger in = new BigInteger(num,16);
return (double) in;
}