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;
    }

猜你喜欢

转载自blog.csdn.net/qq_32136827/article/details/84303422
今日推荐