#include <stdio.h> #include <stdio.h> int htoi(char s[]); main() { char s1[] = "10"; char s2[] = "2D"; char s3[] = "3f"; char s4[] = "0X4F"; char s5[] = "0x3a"; printf("%s -> %d ", s1, htoi(s1)); printf("%s -> %d ", s2, htoi(s2)); printf("%s -> %d ", s3, htoi(s3)); printf("%s -> %d ", s4, htoi(s4)); printf("%s -> %d ", s5, htoi(s5)); } int htoi(char s[]) { int n = 0; int i = -1; while (s[++i] != ' ') { if (i == 0 && s[i] == '0') continue; else if (s[i] == 'x' || s[i] == 'X') continue; else if ('0'<= s[i] && s[i] <= '9') n = n * 16 + (s[i] - '0'); else if ('a'<= s[i] && s[i] <= 'f') n = n * 16 + (s[i] - 'a' + 10); else if ('A' <= s[i] && s[i] <= 'F') n = n * 16 + (s[i] - 'A' + 10); else return -1; } return n; } 程序转自:http://blog.csdn.net/dc_726/article/details/7032656