Exercise 3.5 - function itob, converts a integer into a string¶
Question¶
Write the function itob(n,s,b) that converts the integer n into a base b character representation in the string s. In particular, itob(n,s,16) formats s as a hexadecimal integer in s.
#include <stdio.h>
#include <string.h>
#define MAXLINE 100
void reverse(char s[]) {
int i, j, c;
for (i = 0, j = strlen(s) - 1; i < j; i++, j--)
c = s[i], s[i] = s[j], s[j] = c;
}
void itob(int n, char s[], int b) {
int i, j, sign;
if ((sign = n) < 0)
n = -n;
i = 0;
do {
j = n % b;
s[i++] = (j <= 9) ? j + '0' : j + 'a' - 10;
} while ((n /= b) > 0);
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}
int main(void) {
int number, base;
char str[MAXLINE];
number = 42425;
base = 16;
itob(number, str, base);
printf("%s", str);
return 0;
}
Explanation¶
In this, we are specifically targetting the conversion to base 16, though we should be able to extend the program to any base.
As before we get the number and store it in sign, then we get the remainder of the number after dividing by base b. We covert the number we have gotten to hexadecimal by this expression ` (j <= 9)?j+’0’:j+’a’-10`, which states that if the number is less than 10, return the string representation of it, otherwise subtract 10 from it and add ‘a’ to get the hexadecimal representation of 10 to 15 that (a,b,c,d,e,f).
We store these in a string and it number was a negative number, we append ‘-’ sign to it. We get the result, by reversing the string which we constructed.