题解 | #进制转换#
进制转换
https://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String value = in.nextLine(); if(value.startsWith("0x")){ value = value.substring(2).toUpperCase(); } int sum = 0; for (int i = 0 ;i< value.length(); i++){ int tempSum = getIntByChar(value.charAt(i)); for(int j = value.length() - i - 1 ; j>0 ; j--){ tempSum = 16 * tempSum; } sum += tempSum; } System.out.println(sum); } /** 根据ascll码返回16进制对应十进制数 */ private static int getIntByChar(char c){ return c >= 65 ? c - 55 : c - 48; } }