/* Print integers in a given base 2-16 (default 10) */ #include #include char *convert(int n, int b, char *str) { if (n/b > 0) str = convert(n/b, b, str); *str++ = "0123456789ABCDEF"[n%b]; return str; } int itoa(int n, int b, char *str) { char *s = convert(n, b, str); *s = '\0'; return s - str; } int main(int argc, char *argv[]) { int i; for (i = 1; i < argc - 1; i += 2) { int n, base, len; char buf[50]; sscanf(argv[i], "%d", &n); sscanf(argv[i+1], "%d", &base); if (base < 2 || base > 16) { printf("%s: %d is a invalid base; using 10\n", argv[0], base); base = 10; } len = itoa(n, base, buf); printf("%s (%d)\n", buf, len); } return 0; }