题目描述
写出一个程序,接受一个十六进制的数值字符串,输出该数值的十进制字符串。(多组同时输入 )
输入描述:
输入一个十六进制的数值字符串。
输出描述:
输出该数值的十进制字符串。
输入
0xA
输出
10
**************************************************************
#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
char str[100];
int i=0,count,sum;
while(gets(str)) //用于多次输入
{
count=strlen(str); //计算字符串的长度
sum=0;
for(i=count-1;i>=0;i--) //从十六进制个位开始,每位都转换成十进制
{
if(str[i]>='0'&&str[i]<='9') //数字字符的转换
{
sum+=(str[i]-48)*pow(16,count-i-1); //字符'0'--'9'的ASCll码是48--57
}
else if(str[i]>='A'&&str[i]<='F') //字母字符的转换
{
sum+=(str[i]-55)*pow(16,count-i-1); //字符‘A’---‘Z’的ASCll是65--90
}
}
printf("%d\n",sum);
}
return 0;
}