大数面试题
2020-03-23 本文已影响0人
抬头挺胸才算活着
- 16进制转10进制
写出一个程序,接受一个十六进制的数值字符串,输出该数值的十进制字符串。(多组同时输入 )
char2Digit.put('0', "0");
char2Digit.put('1', "1");
char2Digit.put('2', "2");
char2Digit.put('3', "3");
char2Digit.put('4', "4");
char2Digit.put('5', "5");
char2Digit.put('6', "6");
char2Digit.put('7', "7");
char2Digit.put('8', "8");
char2Digit.put('9', "9");
char2Digit.put('A', "10");
char2Digit.put('B', "11");
char2Digit.put('C', "12");
char2Digit.put('D', "13");
char2Digit.put('E', "14");
char2Digit.put('F', "15");
Scanner in = new Scanner(System.in);
while (in.hasNextLine()){
String hexString = in.nextLine();
hexString = hexString.substring(2);
BigInteger num = new BigInteger("0");
// Integer num = Integer.valueOf(hexString, 16);
for (int i = 0; i < hexString.length(); i++) {
char ch = hexString.charAt(i);
num = num.multiply(new BigInteger("16"));
num = num.add(new BigInteger(char2Digit.get(ch)));
}
System.out.println(num.toString());
}