Resource limit
Time limit: 1.0s
Memory limit: 512.0MB
Problem description
Input a positive hexadecimal number string with no more than 8 digits from the keyboard, convert it to a positive decimal number and output it.
Note: 10~15 in hexadecimal numbers are represented by uppercase English letters A, B, C, D, E, and F respectively.
Sample input
FFFF
Sample output
65535
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
long ans=0;
int dig = str.length()-1;
for(int i=0;i<str.length();i++){
char c = str.charAt(i);
int x = 0;
switch(c){
case '0': x = 0;break;
case '1': x = 1;break;
case '2': x = 2;break;
case '3': x = 3;break;
case '4': x = 4;break;
case '5': x = 5;break;
case '6': x = 6;break;
case '7': x = 7;break;
case '8': x = 8;break;
case '9': x = 9;break;
case 'A': x = 10;break;
case 'B': x = 11;break;
case 'C': x = 12;break;
case 'D': x = 13;break;
case 'E': x = 14;break;
case 'F': x = 15;break;
}
ans = ans+x*((long)Math.pow(16, dig));
dig--;
}
System.out.println(ans);
}
}