在Linux GCC编译测试通过,代码如下: | |
~~~.c | |
#include <stdio.h> | |
void tobase(int, int); | |
int main() | |
{ | |
tobase(33, 16); | |
return 0; | |
} | |
void tobase(int value, int base) | |
{ | |
static char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
char buf[(sizeof(long) << 3) + 1]; | |
char *ptr, *end; | |
if (base < 2 || base > 36) { | |
printf("error base range \n"); | |
} | |
end = ptr = buf + sizeof(buf) - 1; | |
*ptr = '\0'; | |
do { | |
*--ptr = digits[value % base]; | |
value /= base; | |
} while (ptr > buf && value); | |
printf("base: %d, => %s \n", base, ptr); | |
} | |
~~~ |
转自:http://www.yinqisen.cn/blog-673.html