题解 | #进制转换#
进制转换
http://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6
#include <stdio.h>
int main() { char str[128] = {0}; int out = 0; int num = -1;
while(gets(str)) //输入0XAB
{
//str[0] = '0' str[1] = 'X' str[2] = 'A' str[3] = 'B'
for(int i = strlen(str)-1; i > 1; i--)
{
num++;
if(str[i] >= 'a' && str[i] <= 'f')
{
out += (str[i] - 'a' + 10) * pow(16, num);
}
else if(str[i] >= 'A' && str[i] <= 'F')
{
out += (str[i] - 'A' + 10) * pow(16, num);
}
else if(str[i] >= '0' && str[i] <= '9')
{
out += (str[i] - '0') * pow(16, num);
}
else
{
printf("data failed!!!\n");
return -1;
}
}
printf("%d\n", out);
}
return 0;
}