蓝桥杯 基础练习 十六进制转十进制 JAVA


import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		StringBuilder sb = new StringBuilder(sc.nextLine());
		int n = sb.length();
		long sum = 0;
		int i = 0;
		while (i < n) {
			char c = sb.charAt(n - 1 - i);
			long k = (long) Math.pow(16, i);
			switch (c) {
			case '0':
				break;
			case '1':
				sum += k;
				break;
			case '2':
				sum += 2 * k;
				break;
			case '3':
				sum += 3 * k;
				break;
			case '4':
				sum += 4 * k;
				break;
			case '5':
				sum += 5 * k;
				break;
			case '6':
				sum += 6 * k;
				break;
			case '7':
				sum += 7 * k;
				break;
			case '8':
				sum += 8 * k;
				break;
			case '9':
				sum += 9 * k;
				break;
			case 'A':
				sum += 10 * k;
				break;
			case 'B':
				sum += 11 * k;
				break;
			case 'C':
				sum += 12 * k;
				break;
			case 'D':
				sum += 13 * k;
				break;
			case 'E':
				sum += 14 * k;
				break;
			case 'F':
				sum += 15 * k;
				break;

			}
			i++;
		}
		System.out.println(sum);

	}

}

    原文作者:进制转换
    原文地址: https://blog.csdn.net/SZStudy/article/details/71077082
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞