字符串输入的16进制转换10进制编程的一些问题
输入的16进制是字符串,要求转换为10进制输出。例如输入0xA,输出10。程序如下。输出结果不对,所以加了temp0和temp1两个变量用来观察,调试时发现,temp0和temp1里不是同一个字符。
另外还发现,如果chs声明得比较大,比如产生chs[100]或者去掉lens = strlen(chs)时,上述两者就一样了,这个问题百思不得其解,求大神指教!!!
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main()
{
char chs[] = "";
int lens;
while(scanf("%s",chs) != EOF)
{
int res = 0;
char tmep0 = chs[2];
lens = strlen(chs);
char temp1 = chs[2];
for(int i = 2;i < lens;i++)
{
if(chs[i] >= 'A')
res += (chs[i] - 55)*pow(16,lens-i-1);
else
res += (chs[i] - '0')*pow(16,lens-i-1);
}
printf("%d\n",res);
}
}

