/* Exercise 3.5 from "The C programming language" book by K&R */
#include <stdio.h>
#include <string.h>
void reverse(char s[])
{
int c, i, j;
for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
/* converts integer n to it symbol representation in
scale of notation with b radix, and put result to s[] */
void itob(char n, char s[], char b)
{
static char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char sign;
char i;
if ((sign = n) < 0)
n = -n;
i = 0;
do
s[i++] = digits[n % b];
while ((n /= b) > 0);
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}
int main(void)
{
char s[100] = "";
itob(123,s,16);
printf("%s\n",s);
return 0;
}
----------