java十六進制轉十進制 帶正負號

原文地址:https://blog.csdn.net/qq_32136827/article/details/84303422

/**
     * 十六進制轉正負數

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章