-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathitodec.c
133 lines (118 loc) · 2.22 KB
/
itodec.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#ifndef mini_itodec_c
#define mini_itodec_c
//+ansi stdio.h
//
//+doc convert int to string.
// prec: precision, e.g. 4=> 0087
// pad: 0 (pad with spaces), or the char to pad
//+def uitodec
int ATTR_OPT("Os")uitodec(unsigned int i, char *buf, int prec, char limiter, char pad ){
int p = 0;
int a;
int t1,t2,t3,dec;
//unsigned int ut1,ut2,ut3;
char n;
char trail=' ';
if ( pad )
trail = pad;
prec = prec - 10; // Maximale Stellenanzahl
if ( i==0 ){
buf[0] = '0';
//p++;
//return(
}
int trailing = 1;
const int div[13] = {0, 100000000, 10000000, 1000000, 0, 100000, 10000, 1000, 0, 100, 10, 1};
if ( i >= 1000000000){
//prints("hier.\n");
p=1;
prec=0;
if ( i>=2000000000 ){
i-=2000000000;
if ( i>=1000000000 ){
if ( i>=2000000000 ){
buf[0] = '4';
i-=2000000000;
} else {
buf[0] = '3';
i-=1000000000;
}
} else {
buf[0] = '2';
//i-=2000000000;
}
} else {
buf[0] = '1';
i-=1000000000;
}
trailing = 0;
} else {
if ( prec == 0 ){
buf[0] = ' ';
p++;
} else
prec++;
}
for (a=0;a<12;a++){
if ( div[a] == 0 ){
if ( (limiter != 0 ) && (prec==0) ){
buf[p] = limiter;
p++;
}
} else {
n = '0';
if ( (t1=(i - div[a])) >= 0 ){
prec = 0;
trailing = 0;
n = '1';
if ( (t2=(t1-(dec=(div[a]<<2)))) >= 0){
if ( (t3=t2-dec) >= 0 ){
n = '9';
i = t3;
goto write;
} else {
n = '5';
t1 = t2;
}
}
if ( (t2=(t1-(div[a]<<1) ) ) >=0 ){
t1=t2;
n += 2;
}
if ( (t2=(t1-(div[a]) ) ) >=0 ){
t1=t2;
n += 1;
}
i = t1;
}
if ( prec == 0 ){
write:
if ( trailing )
buf[p] = trail;
else
buf[p] = n;
p++;
} else
prec++;
//buf[p] = 'x';
//p++;
}
}
if ( p==0 ){
buf[0]='0';
p=1;
}
buf[p] = 0;
return(p);
}
//+depends uitodec
//+def
int itodec(int i, char *buf, int prec, char limiter, char pad ){
if ( i < 0 ){
buf[0]='-';
i = -i;
return(uitodec((unsigned int)i,&buf[1],prec,limiter,pad) + 1);
}
return(uitodec((unsigned int)i,buf,prec,limiter,pad) );
}
#endif