/* converts the integer n into a base b representation */ #define abs(x) ((x) > 0 ? (x) : -(x)) void itob(int n, char *s, int b) { char charset[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' } int maxbase = (sizeof charset) / sizeof (char); int sign; if (b > maxbase) { *s = '\0'; return; } sign = n; do { *s++ = charset[abs(n % b)]; } while ((n /= b) > 0); if (n < 0) *s++ = '-'; *s = '\0'; reverse(s); }