Java10进制转16进制,16进制转10进制

1、Java10进制转16进制

/** 卡号位数:8 */
    public static byte CARD_NUM_BIT = 8;
 
/**
     * isBlank 
     * 
     * @param value
     * @return true: blank; false: not blank
     */
    private static boolean isBlank(String value) {
        if (value == null || "".equals(value.trim())) {
            return true;
        }
        return false;
    }
 
/**
     * 10进制转16进制,并补齐卡号位数
     * 
     * @param str
     * @return
     */
    public static String toHexStr(String str) {
        String result = "";
        String regex = "^\\d{1,}$";
        if (!isBlank(str)) {
            str = str.trim();
            if (str.matches(regex)) {
                String hexStr = Long.toHexString(Long.parseLong(str.trim())).toUpperCase();
                if (hexStr.length() < CARD_NUM_BIT) {
                    hexStr = org.apache.commons.lang3.StringUtils.leftPad(hexStr, CARD_NUM_BIT, '0');
                }
                result = hexStr;
            } else if (isHex(str)) {
                if (str.length() < CARD_NUM_BIT) {
                    str = org.apache.commons.lang3.StringUtils.leftPad(str, CARD_NUM_BIT, '0');
                }
                result = str;
            }
        }
        return result;
    }

2、Java16进制转10进制

/**
     * isBlank 
     * 
     * @param value
     * @return true: blank; false: not blank
     */
    private static boolean isBlank(String value) {
        if (value == null || "".equals(value.trim())) {
            return true;
        }
        return false;
    }
 
/**
     * 判断是否是16进制数
     * 
     * @param strHex
     * @return
     */
    public static boolean isHex(String strHex) {
        int i = 0;
        if (strHex.length() > 2) {
            if (strHex.charAt(0) == '0' && (strHex.charAt(1) == 'X' || strHex.charAt(1) == 'x')) {
                i = 2;
            }
        }
        for (; i < strHex.length(); ++i) {
            char ch = strHex.charAt(i);
            if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F') || (ch >= 'a' && ch <= 'f'))
                continue;
            return false;
        }
        return true;
    }
 
/**
     * 计算16进制对应的数值
     * 
     * @param ch
     * @return
     * @throws Exception
     */
    private static int getHex(char ch) throws Exception {
        if (ch >= '0' && ch <= '9')
            return (int) (ch - '0');
        if (ch >= 'a' && ch <= 'f')
            return (int) (ch - 'a' + 10);
        if (ch >= 'A' && ch <= 'F')
            return (int) (ch - 'A' + 10);
        throw new Exception("error param");
    }
 
/**
     * 计算幂
     * 
     * @param nValue
     * @param nCount
     * @return
     * @throws Exception
     */
    private static long getPower(int nValue, int nCount) throws Exception {
        if (nCount < 0)
            throw new Exception("nCount can't small than 1!");
        if (nCount == 0)
            return 1;
        long nSum = 1;
        for (int i = 0; i < nCount; ++i) {
            nSum = nSum * nValue;
        }
        return nSum;
    }
 
/**
     * 16进制转10进制,对16进制数的每一位数乘以其对应的16的幂,相加。
     * @param strHex 待转换的字符串
     * @param force 是否强制按16进制转换,纯数字也可能是16进制,true则将纯数字按16进制处理
     * @return
     */
    public static long hexToLong(String strHex, boolean force) {
        long nResult = 0;
        String regex = "^\\d{1,}$";
        if (!isBlank(strHex)) {
            strHex = strHex.trim();
        } else {
            return nResult;
        }
        if (!force && strHex.matches(regex)) {
            return Long.parseLong(strHex);
        }
        if (!isHex(strHex)) {
            return nResult;
        }
        String str = strHex.toUpperCase();
        if (str.length() > 2) {
            if (str.charAt(0) == '0' && str.charAt(1) == 'X') {
                str = str.substring(2);
            }
        }
        int nLen = str.length();
        for (int i = 0; i < nLen; ++i) {
            char ch = str.charAt(nLen - i - 1);
            try {
                nResult += (getHex(ch) * getPower(16, i));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return nResult;
    }
 
/**
     * 16进制转10进制
     * @param strHex
     * @return
     */
    public static long hexToLong(String strHex) {
        return hexToLong(strHex, false);
    }
 

https://blog.csdn.net/jinqilin721/article/details/94429799

Java 
更新时间:2020-05-21 19:36:08

本文由 新逸Cary 创作,如果您觉得本文不错,请随意赞赏
采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
原文链接:https://blog.xinac.cn/archives/java10进制转16进制16进制转10进制.html
最后更新:2020-05-21 19:36:08

评论

Your browser is out of date!

Update your browser to view this website correctly. Update my browser now

×