|
2 | 2 | * Exercise 3-5. Write the function itob(n,s,b) that converts the integer n
|
3 | 3 | * into a base b character representation in the string s. In particular,
|
4 | 4 | * itob(n,s,16) formats s as a hexadecimal integer in s.
|
| 5 | + * |
5 | 6 | * By Faisal Saadatmand
|
6 | 7 | */
|
7 | 8 |
|
| 9 | +#include <limits.h> |
8 | 10 | #include <stdio.h>
|
9 | 11 | #include <string.h>
|
10 | 12 |
|
| 13 | +#define MAXLEN 1000 |
| 14 | + |
11 | 15 | /* functions */
|
12 | 16 | void reverse(char []);
|
13 |
| -void itob(int, char [], int); |
| 17 | +void itob(unsigned, char [], int); |
14 | 18 |
|
15 |
| -/* reverse function: reverse string s in place */ |
| 19 | +/* reverse: reverse string s in place */ |
16 | 20 | void reverse(char s[])
|
17 | 21 | {
|
18 | 22 | int c, i, j;
|
19 | 23 |
|
20 |
| - for (i = 0, j = strlen(s) - 1; i < j; i++, j --) { |
| 24 | + for (i = 0, j = strlen(s) - 1; i < j; i++, j--) { |
21 | 25 | c = s[i];
|
22 | 26 | s[i] = s[j];
|
23 | 27 | s[j] = c;
|
24 |
| - --j; |
25 | 28 | }
|
26 | 29 | }
|
27 | 30 |
|
28 |
| -/* itob: convert number into a base base character representation in the string s */ |
29 |
| -void itob(int number, char convertedNumber[], int base) |
| 31 | +/* itob: converts the integer n into a base b character representation in the |
| 32 | + * string s. */ |
| 33 | +void itob(unsigned n, char s[], int b) |
30 | 34 | {
|
31 |
| - int i, sign; |
32 |
| - unsigned int uNumber, nextDigit; |
33 |
| - const char baseDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', |
34 |
| - '9', 'A', 'B', 'C', 'D', 'E', 'F' }; |
35 |
| - |
36 |
| - sign = 0; |
37 |
| - if (number < 0) /* record sign */ |
38 |
| - sign = 1; |
| 35 | + int i, sign, digit; |
| 36 | + const char baseDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', |
| 37 | + '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; |
39 | 38 |
|
40 |
| - uNumber = (unsigned) number; /* work in two's complement number system */ |
41 |
| - |
42 |
| - if (sign > 0 && base == 10) /* convert back to decimal for base 10 */ |
43 |
| - uNumber = ~uNumber + 1; |
| 39 | + if (b == 10 && (sign = n) < 0) |
| 40 | + n = -n; /* or you can use ~n + 1 */ |
44 | 41 |
|
45 | 42 | i = 0;
|
46 |
| - do { /* generate digits in reverse order */ |
47 |
| - nextDigit = uNumber % base; /* get next digit */ |
48 |
| - uNumber /= base; |
49 |
| - convertedNumber[i] = baseDigits[nextDigit]; |
50 |
| - ++i; |
51 |
| - } while (uNumber != 0); |
| 43 | + do { /* generate digits in reverse order */ |
| 44 | + digit = n % b; /* get next digit */ |
| 45 | + s[i++] = baseDigits[digit]; |
| 46 | + } while ((n /= b) > 0); |
52 | 47 |
|
53 |
| - if (sign > 0 && base == 10) /* add sign symbol for base 10 */ |
54 |
| - convertedNumber[i++] = '-'; |
| 48 | + if (b == 10 && sign < 0) /* add sign symbol for base 10 */ |
| 49 | + s[i++] = '-'; |
55 | 50 |
|
56 |
| - convertedNumber[i] = '\0'; |
| 51 | + s[i] = '\0'; |
57 | 52 |
|
58 |
| - reverse(convertedNumber); |
| 53 | + reverse(s); |
59 | 54 | }
|
60 | 55 |
|
61 | 56 | int main(void)
|
62 | 57 | {
|
63 |
| - int number, base; |
64 |
| - char stringNumber[64]; |
65 |
| - |
66 |
| - printf("Enter an integer to convert: "); |
67 |
| - scanf("%i", &number); |
68 |
| - |
69 |
| - printf("Enter base: "); |
70 |
| - scanf("%i", &base); |
71 |
| - |
72 |
| - itob(number, stringNumber, base); |
73 |
| - |
74 |
| - printf("%s\n", stringNumber); |
| 58 | + int i; |
| 59 | + char str[MAXLEN]; |
| 60 | + |
| 61 | + printf("converting %i and %i to\n", INT_MIN, INT_MAX); |
| 62 | + for (i = 2; i <= 16; ++i) { |
| 63 | + printf("Base% i:\n", i); |
| 64 | + itob(INT_MIN, str, i); |
| 65 | + printf(" %s\n", str); |
| 66 | + itob(INT_MAX, str, i); |
| 67 | + printf(" %s\n", str); |
| 68 | + } |
75 | 69 |
|
76 | 70 | return 0;
|
77 | 71 | }
|
0 commit comments