题解 | #进制转换#
进制转换
http://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
* @ProjectName: leetcode
* @Package: com.company.niuke
* @ClassName: SysConvert
* @Author: Administrator
* @Description: 进制转换
* @Date: 2022/2/11 23:13
* @Version: 1.0
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Map<Character, Integer> map = new HashMap<>();
map.put('A',10);
map.put('B',11);
map.put('C',12);
map.put('D',13);
map.put('E',14);
map.put('F',15);
while (scanner.hasNext()) {
String s = scanner.nextLine(); //十六进制数
char[] ox = s.replace("0x", "").toUpperCase().toCharArray();
int result = 0;
for (int i = ox.length-1; i >= 0; i--) {
if(map.containsKey(ox[i])) {
result += map.get(ox[i]) * Math.pow(16,ox.length-1-i);
}else {
result += Integer.valueOf(String.valueOf(ox[i])) * Math.pow(16,ox.length-1-i);
}
}
System.out.println(result);
}
}
}