写出一个程序,接受一个十六进制的数值字符串,输出该数值的十进制字符串。(多组同时输入 )
输入描述:
输入一个十六进制的数值字符串。
输出描述:
输出该数值的十进制字符串。
输入例子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;
}
最后
以上就是淡然唇彩最近收集整理的关于笔试题-进制转换的全部内容,更多相关笔试题-进制转换内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复