#line 2
#include <stdio.h>
#include <stdlib.h>
char *int_to_string(int n, int radix) {
static const char const digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()[]{}<>_";
static char buf[512];
size_t i;
int isneg;
if (radix < 2 || radix > sizeof(digits)) {
printf("...");
return (char *)NULL;
}
isneg = 0;
if (n < 0) {
isneg = 1;
n = -n;
}
i = sizeof(buf) - 1;
buf[i--] = '\0';
do {
buf[i--] = digits[n%radix];
n = n/radix;
} while(n > 0);
if (isneg)
buf[i--] = '-';
return &buf[i + 1];
}
void test_(unsigned int line, const char *sn, int n, int radix) {
char *p;
printf("[%u](%s; %i): ", line, sn, radix);
p = int_to_string(n, radix);
if (!p)
printf("invalid parameter\n");
else
printf("%s\n", p);
fflush(stdout);
}
#define test(n,radix) test_(__LINE__,#n,(n),(radix))
int main() {
test(42, 10);
test(23, 10);
test(32, 16);
test(0777, 8);
test(0xFACEBEEF, 16);
test(0x0B5E55ED, 16);
test(1234, 10);
test(20120830, 36);
test(20121212, 36);
test(0, 10);
test(-43, 10);
test(20121212, 47);
test(20121212, 54);
test(0xA, 2);
return EXIT_SUCCESS;
}