写出一个程序,接受一个十六进制的数值字符串,输出该数值的十进制字符串。(多组同时输入 )
输入描述:
输入一个十六进制的数值字符串。
输出描述:
输出该数值的十进制字符串。
输入例子1:
0xA
输出例子1:
10
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int main(void)
{
string hex_str;
while(cin>>hex_str)
{
int hex_num=0;
int str_len=hex_str.length();
for(int i=str_len-1;i>1;--i)
{
if((hex_str[i]>=65)&&(hex_str[i]<=90))
{
hex_num+=(10+hex_str[i]-65)*pow(16,str_len-1-i);
}
else if((hex_str[i]>=97)&&(hex_str[i]<=122))
{
hex_num+=(10+hex_str[i]-97)*pow(16,str_len-1-i);
}
else if((hex_str[i]>=48)&&(hex_str[i]<=57))
{
hex_num+=(hex_str[i]-48)*pow(16,str_len-1-i);
}
}
cout<<hex_num<<endl;
}
return 0;
}