标签:mamicode 过程 lazy std 代码 大小写 scan alt res
转换的过程需要乘的基数为16,其次注意十六进制中的a~f的字母转换,并且无论大小写都要能够转换。
#include<stdio.h>
size_t HextoDec(char s[])
{
size_t i, n;
n = 0;
for (i = 0; s[i] != ‘\0‘; i++)
{
if (s[i] >= ‘0‘&& s[i] <= ‘9‘)
n = n * 16 + s[i] - ‘0‘;
if (s[i] >= ‘a‘ && s[i] <= ‘f‘)
n = n * 16 + s[i] - ‘a‘ + 10;
if (s[i] >= ‘A‘ && s[i] <= ‘F‘)
n = n * 16 + s[i] - ‘A‘ + 10;
}
return n;
}
int main()
{
size_t result = 0;
char hex[9] = {0};
printf("input a HEX number:");
scanf("%s", hex);
result = HextoDec(hex);
printf("0x%s = %u\n", hex, result);
return 0;
}
标签:mamicode 过程 lazy std 代码 大小写 scan alt res
原文地址:https://www.cnblogs.com/inta/p/13362520.html