这里记一句,如果是long型或unsigned long型的数,可以直接printf("%ul\%l");的方式直接输出出来。或者可以使用sprintf (buf,"%x\%ul\%l",dex);这种方式将十六进制数存放在一个数组里,然后再使用如下的函数。
#include <stdio.h>
#include <stdlib.h>#include <math.h>int toi(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",toi("0xff"));}