/* Print integers in a given base 2-16 (default 10) */ #include #include int itoa(int n, int b, char *str) { char buf[64], *s = &buf[64]; *s = '\0'; do *--s = "0123456789ABCDEF"[n%b]; while ((n /= b) > 0); strcpy(str, s); return &buf[64] - s; } 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; }