#include <stdio.h> #include <stdlib.h> /*十六进制字符串转成对应整型值*/ int htoi(char s[]) { int i; int n = 0; if (s[0] == '0' && (s[1]=='x' || s[1]=='X')) { i = 2; } else { i = 0; } for (; (s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >='A' && s[i] <= 'Z');++i) { if (tolower(s[i]) > '9') { n = 16 * n + (10 + tolower(s[i]) - 'a'); } else { n = 16 * n + (tolower(s[i]) - '0'); } } return n; } int main() { printf("%d\n",htoi("A")); return 0; } 运行结果: 10 |
c语言_十六进制字符串转成对应整型值
最新推荐文章于 2024-05-31 16:14:57 发布