The C Programming Language, 2nd Edition, by Kernighan and Ritchie
Exercise 3.05 on page 64
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 s
.
Solution by Vidhan Gupta
/* 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 s. */ #include<stdio.h> #include<string.h> void itob(int n , char s[], int b); void reverse(char s[]); #define MAXIMUM 50 int main(){ int b = 16; char s[MAXIMUM]; itob(123456,s,b); printf("Base %d = %s\n",b,s); return 0; } void itob(int n, char s[], int b){ int i = 0; do { if (n%b>9) s[i++]= n%b +'A'-10; else s[i++] = n%b +'0'; } while ((n/=b)); reverse(s); } 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; }
OUTPUT: Base 16 = 1E240
Solution by Paul Griffiths
/* EX3_5.C ======= Suggested solution to Exercise 3-5 */ #include <stdlib.h> #include <stdio.h> void itob(int n, char s[], int b); void reverse(char s[]); int main(void) { char buffer[10]; int i; for ( i = 2; i <= 20; ++i ) { itob(255, buffer, i); printf("Decimal 255 in base %-2d : %s\n", i, buffer); } return 0; } /* Stores a string representation of integer n in s[], using a numerical base of b. Will handle up to base-36 before we run out of digits to use. */ void itob(int n, char s[], int b) { static char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; int i, sign; if ( b < 2 || b > 36 ) { fprintf(stderr, "EX3_5: Cannot support base %d\n", b); exit(EXIT_FAILURE); } 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); } /* Reverses string s[] in place */ 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; } }
Solution by seankndy
#include <stdio.h> void itob(int n, char s[], int b); void reverse(char s[]); int main(void) { int n; char s[100]; for (int i = 2; i <= 20; i++) { itob(255, s, i); printf("decimal 255 in base %-2d : %s\n", i, s); } return 0; } void itob(int n, char s[], int b) { int i, sign, r; sign = n; i = 0; do { r = n % b; if (sign < 0) r = -r; s[i++] = (r > 9 ? (r-10 + 'A') : (r + '0')); } while (n /= b); if (sign < 0) s[i++] = '-'; s[i] = '\0'; reverse(s); } void reverse(char s[]) { int i, j, t; for (j = 0; s[j] != '\0'; j++) ; for (i = 0, --j; j > i; i++, j--) { t = s[j]; s[j] = s[i]; s[i] = t; } }
Solution by Luke Panayi
My solution with additional functions included:
/* 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 s. */ /* max base of 32 */ #include <stdio.h> #define BUFFER 1000 int reverse(char s[]) { char ch; int i, j; for(j = 0; s[j] != '\0'; j++); --j; for(i = 0; i < j; i++) { ch = s[i]; s[i] = s[j]; s[j] = ch; --j; } return 0; } void itob(int n, char s[], int b) { int i, j, sign, c; unsigned k = 0; if ((sign=n) < 0) { k = -n; } else { k = n; } i = 0; do { j = k % b; s[i++] = (j < 9) ? (j + '0'):((j-10) + 'A'); } while (k /= b); if (sign < 0) s[i++] = '-'; s[i] = '\0'; reverse(s); } int main() { char s[BUFFER]; itob(255, s, 32); printf("%s\n", s); return 0; }
Solution by Miguel Degrossoli
/* 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 s as a hexadecimal integer in s. */ #include <stdlib.h> #include <stdio.h> #include <limits.h> #define MAXLEN 32 /* an integer has 32-bits in my machine */ #define BASE_MIN 2 /* well, you cannot go lower */ #define BASE_MAX 36 /* amount of alphanumeric chars available */ void itob(int n, char s[], char b); void reverse(char s[]); int main() { int n; /* I'm getting its value from the trash */ char s[MAXLEN + 1], b; printf("Value of %d in...\n", n); for (b = BASE_MIN; b <= BASE_MAX; b++) { itob(n, s, b); printf("Base %d: %s\n", b, s); } exit(0); } void itob(int n, char s[], char b) { int i, j, c; if (b < BASE_MIN || b > BASE_MAX) { printf("Valid base ranges from %d to %d.\n", BASE_MIN, BASE_MAX); exit(1); } j = 0; if (n == 0) s[j++] = '0'; for (i = n; i > 0; i /= b) { if ((c = i % b) <= 9) s[j++] = c + '0'; else s[j++] = c - 10 + 'A'; } s[j++] = '\0'; reverse(s); } void reverse(char s[]) { int i, n; char line[MAXLEN + 1]; for (i = 0; s[i] != '\0' && i < MAXLEN; ++i) ; n = 0; if (i == 0 || s[i] == '\0') line[i] = '\0'; if (i > 0) { for (--i; i >=0; --i) { line[i] = s[n]; n++; } for (i = 0; line[i] != '\0'; i++) s[i] = line[i]; } }
Sample:
miguel@Miguel-Notebook:~/Desenvolvimento/C$ ./exercise_3-5 Value of 22087 in... Base 2: 101011001000111 Base 3: 1010022001 Base 4: 11121013 Base 5: 1201322 Base 6: 250131 Base 7: 121252 Base 8: 53107 Base 9: 33261 Base 10: 22087 Base 11: 1565A Base 12: 10947 Base 13: A090 Base 14: 8099 Base 15: 6827 Base 16: 5647 Base 17: 4874 Base 18: 3E31 Base 19: 3439 Base 20: 2F47 Base 21: 281G Base 22: 21DL Base 23: 1IH7 Base 24: 1E87 Base 25: 1A8C Base 26: 16HD Base 27: 1381 Base 28: 104N Base 29: Q7I Base 30: OG7 Base 31: MUF Base 32: LI7 Base 33: K9A Base 34: J3L Base 35: I12 Base 36: H1J