-
Notifications
You must be signed in to change notification settings - Fork 0
/
itob.c
59 lines (45 loc) · 1.09 KB
/
itob.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* 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 "stdio.h"
#include "limits.h"
#include "string.h"
#define MAX 100
#define NUM -234561
#define BASE 20 // max to 36
void itob(int n, char s[], unsigned b);
void strrev(char s[]);
int main(void) {
int n = NUM;
char s[MAX];
unsigned b = BASE;
printf("Decimal: %d\n", n);
itob(n, s, b);
printf("Base %u: %s\n", b, s);
return 0;
}
void itob(int n, char s[], unsigned b) {
enum signs {POS, NEG};
int i = 0;
int sign = n<0 ? NEG : POS;
unsigned un = n==INT_MIN || n>0 ? n : -n;
do {
s[i] = un % b;
s[i] += s[i]<10 ? '0' : 'a'-10;
i++;
} while ((un /= b) > 0);
if (sign == NEG)
s[i++] = '-';
s[i] = '\0';
strrev(s);
}
/* String reverser */
void strrev(char s[]) {
size_t i, j;
char temp;
for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}