要注意输入可能是个字符串,还有大小写识别。
#include <stdio.h>
#include <math.h>
int HexToDec(char a[100])
{
int nDec=0;
for(int i = 0; a[i] != '\0';i++)
{
char c = a[i];
int n = 0;
if( c >= '0' && c <='9')
n = c-48;
else if( c == 'a' || c == 'A')
n = 10;
else if (c == 'b' || c == 'B')
n = 11;
else if (c == 'c' || c == 'C')
n = 12;
else if (c == 'd' || c == 'D')
n = 13;
else if (c == 'e' || c == 'E')
n = 14;
else if (c == 'F' || c == 'f')
n = 15;
nDec = nDec*16+n;
}
return nDec;
}
void main()
{
char a[100],b[100];
while(scanf("%s %s",&a,&b) != EOF)
{
int a1 = HexToDec(a);
int b1 = HexToDec(b);
int sum = a1+b1;
printf("%d\n",sum);
}
}