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

本文介绍了如何在Java中进行10进制到16进制以及16进制到10进制的转换。首先讲解了使用内置函数将10进制数转换为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);
    }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值