题解 | #进制转换#
进制转换
https://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
import java.io.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input;
// 读取输入,直到没有更多输入为止
while ((input = br.readLine()) != null) {
// 提取十六进制字符串(去除前缀0x)
String hexString = input.substring(2);
int decimalValue = 0;
int length = hexString.length();
// 遍历十六进制字符串计算对应的十进制数值
for (int i = 0; i < length; i++) {
char c = hexString.charAt(i);
// 将字符转换为对应的数字
int tempNum = Character.digit(c, 16);
// 使用累加的方式计算十进制数值
decimalValue = decimalValue * 16 + tempNum;
}
// 输出对应的十进制数值
System.out.println(decimalValue);
}
}
}

