Exercise 3-5 (itob - Integer to base)
Chapter_3 Exercise_3-4 utoa | Exercise_3-6 |
Exercise 3-5 K&R, p. 64
Exercise 3-5. 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 n as a hexadecimal integer in the string s.
itob.c download
#include <stdio.h> // for printf(), scanf()
#define SIZE 100 // array size (max no of digits)
// reverse s[], knowing its length:
void reverse(char s[], int len);
int itob (int n, char s[], int b); // int n to string of chars, base b
int main()
{
char digits[SIZE];
int n, base;
printf ("Give an integer: ");
scanf ("%d", &n);
printf ("Convert to base (2 <= base <= 36): ");
scanf ("%d", &base); // 0..9a..z = 36 chars
if (base < 2 || base > 36)
{
printf ("2 <= base <= 36\n");
printf ("Your input: %d\n", base);
return 1; // end program with an error message
}
itob(n, digits, base);
printf ("In base %d: %s\n", base, digits); // with an eventual sign
return 0;
}
// reverse s[], knowing its length:
void reverse(char s[], int len)
{
int i = 0, j = len-1;
char temp;
while (i < j)
{
temp = s[i];
s[i] = s[j];
s[j] = temp;
i++;
j--;
}
}
// get the digits of n into s[] (convert n to a string of characters), base b,
// return the length of s[] (no of digits, eventual sign):
int itob (int n, char s[], int b)
{ // both base b and remainder must be int, not unsigned, to prevent
// automatic conversion from int to unsigned in case n < 0
int i = 0, sign = 1;
int remainder;
if (n < 0)
{
sign = -1;
}
do
{ // get the digits in reverse order:
remainder = (n % b) * sign; // if n < 0, (n % b) < 0
// if remainder or b unsigned, n < 0 converted to large positive number
if (remainder < 10)
{s[i++] = remainder + '0';} // 0..9
else
{s[i++] = remainder - 10 + 'a';} // a..z
// if n < 0, (n /= b) <= 0
} while (n /= b); // while ((n /= b) != 0); // delete last digit
// if b unsigned, n < 0 converted to large positive (unsigned) number
if (sign < 0) {s[i++] = '-';} // first char after reverse
s[i] = '\0'; // end the string
reverse(s, i);
return i;
}
/*
gcc itob.c -o itob
./itob
Give an integer: 0
Convert to base (2 <= base <= 36): 1
2 <= base <= 36
Your input: 1
./itob
Give an integer: 1
Convert to base (2 <= base <= 36): 37
2 <= base <= 36
Your input: 37
./itob
Give an integer: 123456
Convert to base (2 <= base <= 36): 36
In base 36: 2n9c
./itob
Give an integer: -123456
Convert to base (2 <= base <= 36): 36
In base 36: -2n9c
./itob
Give an integer: 123456
Convert to base (2 <= base <= 36): 2
In base 2: 11110001001000000
./itob
Give an integer: -123456
Convert to base (2 <= base <= 36): 2
In base 2: -11110001001000000
./itob
Give an integer: 32767
Convert to base (2 <= base <= 36): 16
In base 16: 7fff
./itob
Give an integer: 32767
Convert to base (2 <= base <= 36): 8
In base 8: 77777
./itob
Give an integer: 32767
Convert to base (2 <= base <= 36): 2
In base 2: 111111111111111
*/
Chapter_3 Exercise_3-4 utoa | BACK_TO_TOP | Exercise_3-6 |
Comments
Post a Comment