您的位置:首页 > 其它

16进制转为10进制

2017-11-13 16:09 295 查看
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: