#include<stdio.h>
int main()
{
char s[20] = "0x12542659";
int i = 0, n = 0;
s[i] == '0' && (s[i + 1] == 'x' || s[i + 1] == 'X') ? i += 2 : i;
for (i = 0; s[i] != '\0'; i++)
{
if (s[i] >= '0' && s[i] <= '9')
n = n * 16 + s[i] - '0';
else if (s[i] >= 'a' && s[i] <= 'f')
n = n * 16 + s[i] - 'a' + 10;
else if (s[i] >= 'A' && s[i] <= 'F')
n = n * 16 + s[i] - 'A' + 10;
}
printf("%s = %d\n", s, n);
return 0;
}
将十六进制数字串转换成整数,假定字符串中不含任何非法字符
最新推荐文章于 2023-07-03 17:12:05 发布