题解 | #进制转换#
进制转换
http://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String string = in.nextLine();
js(string);
}
}
static void js(String str) {
String string = str.replace("0x", "");
String[] str16 = string.split("");
int c = 0;
for (int i = str16.length - 1,j = 0; i >= 0; i--,j++) {
switch (str16[i]) {
case "A":
c+= 10*(Math.pow(16,j));
break;
case "B":
c+= 11*(Math.pow(16,j));
break;
case "C":
c+= 12*(Math.pow(16,j));
break;
case "D":
c+= 13*(Math.pow(16,j));
break;
case "E":
c+= 14*(Math.pow(16,j));
break;
case "F":
c+= 15*(Math.pow(16,j));
break;
default:
c+= Integer.parseInt(str16[i])*(Math.pow(16,j));
}
}
System.out.println(c);
}