java 十六进制负数_Java 十六进制转十进制正负数

/**

* 十六进制转正负数

* (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;

}

你可能感兴趣的:(java,十六进制负数)