一道机试题,十六进制数转化成十进制输出

package com.company; import java.util.Scanner; /** * Created by hemingway on 2018/9/26. */ public class Main1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); if(s.substring(0,2).equals("Ox")){ String ss = s.substring(2,s.length()); transfer(ss); } transfer(s); }private static void transfer(String s){ Long N = 0L; //Math.pow( 10, i); long m = 0L; for(int i = s.length()-1; i > 0; i--){ switch (s.charAt(i)){ case '0': m = new Double(Math.pow( 16, s.length() - i - 1)).longValue(); N = N + 0L * m; break; case '1': m = new Double(Math.pow( 16, s.length() - i - 1)).longValue(); N = N + 1L * m; break; case '2': m = new Double(Math.pow( 16, s.length() - i - 1)).longValue(); N = N + 2L * m; break; case '3': m = new Double(Math.pow( 16, s.length() - i - 1)).longValue(); N = N + 13L * m; break; case '4': m = new Double(Math.pow( 16, s.length() - i - 1)).longValue(); N = N + 4L * m; break; case '5': m = new Double(Math.pow( 16, s.length() - i - 1)).longValue(); N = N + 15L * m; break; case '6': m = new Double(Math.pow( 16, s.length() - i - 1)).longValue(); N = N + 6L * m; break; case '7': m = new Double(Math.pow( 16, s.length() - i - 1)).longValue(); N = N + 7L * m; break; case '8': m = new Double(Math.pow( 16, s.length() - i - 1)).longValue(); N = N + 8L * m; break; case '9': m = new Double(Math.pow( 16, s.length() - i - 1)).longValue(); N = N + 9L * m; break; case 'A': m = new Double(Math.pow( 16, s.length() - i - 1)).longValue(); N = N + 10L * m; break; case 'B': m = new Double(Math.pow( 16, s.length() - i - 1)).longValue(); N = N + 11L * m; break; case 'C': m = new Double(Math.pow( 16, s.length() - i - 1)).longValue(); N = N + 12L * m; break; case 'D': m = new Double(Math.pow( 16, s.length() - i - 1)).longValue(); N = N + 13L * m; break; case 'E': m = new Double(Math.pow( 16, s.length() - i - 1)).longValue(); N = N + 14L * m; break; case 'F': m = new Double(Math.pow( 16, s.length() - i - 1)).longValue(); N = N + 15L * m; break; } } System.out.print(N); }}

System.out.println(Integer.parseInt(str.substring(2),16));

    推荐阅读