16进制转为10进制

来源:互联网 发布:2017百度春运大数据 编辑:程序博客网 时间:2024/06/02 00:22
package tOne;import java.util.HashMap;import java.util.Map;public class myTestOne{public static void main(String[] args) {    HexToDec("2525");}public static long HexToDec(String hexStr) {        Map<String, Integer> hexMap = prepareDate(); // 先准备对应关系数据        int length = hexStr.length();        long result = 0L; // 保存最终的结果        for (int i = 0; i < length; i++) {            result += hexMap.get(hexStr.subSequence(i, i + 1)) * Math.pow(16, length - 1 - i);        }        System.out.println("hexStr=" + hexStr + ",result=" + result);        return result;    }     /**     * 准备十六进制字符对应关系。如("1",1)...("A",10),("B",11)     */    private static HashMap<String, Integer> prepareDate() {        HashMap<String, Integer> hashMap = new HashMap<String, Integer>();        for (int i = 1; i <= 9; i++) {            hashMap.put(i + "", i);        }        hashMap.put("a", 10);        hashMap.put("b", 11);        hashMap.put("c", 12);        hashMap.put("d", 13);        hashMap.put("e", 14);        hashMap.put("f", 15);        return hashMap;    }}

原创粉丝点击