问题描述
从键盘输入一个不超过8位的正的十六进制数字符串,将它转换为正的十进制数后输出。
注:十六进制数中的10~15分别用大写的英文字母A、B、C、D、E、F表示。
样例输入
FFFF
样例输出
65535
#include <iostream>
#include <string>
#include <stdio.h>
#include <math.h>
using namespace std;
int main(){
string s1,s2;
cin >> s1 ;
s2="";
for(int i=0 ; i < s1.length() ; i++){
switch (s1[i])
{
case '1': s2 += "01";break;
case '2': s2 += "02";break;
case '3': s2 += "03";break;
case '4': s2 += "04";break;
case '5': s2 += "05";break;
case '6': s2 += "06";break;
case '7': s2 += "07";break;
case '8': s2 += "08";break;
case '9': s2 += "09";break;
case 'A': s2 += "10";break;
case 'B': s2 += "11";break;
case 'C': s2 += "12";break;
case 'D': s2 += "13";break;
case 'E': s2 += "14";break;
case 'F': s2 += "15";break;
default:
break;
}
}
long long p=0;
int cnt=0;
for(int j=s2.length()-1;j>=0;j-=2,cnt++){
long long x = pow(16,cnt);
p += x* ((s2[j] - '0') + 10*(s2[j-1] - '0'));
}
cout<<p<<endl;
system("pause");
return 0;
}