|
|
1.1 root 1: /*
2: * integer to ascii conversion
3: */
4:
5: #include "fio.h"
6:
7: #define digit(c) ( (c > 9) ? (c - 10 + 'a') : c + '0')
8:
9: char *icvt(value,ndigit,sign) long value; int *ndigit,*sign;
10: {
11: static char buf[MAXINTLENGTH+1];
12: register int i;
13: long kludge, rem, mask = 0x7fffffff;
14: int one = 1;
15: char c;
16:
17: if (value == 0)
18: { *sign=0;
19: *ndigit=one;
20: buf[MAXINTLENGTH]='0';
21: return(&buf[MAXINTLENGTH]);
22: }
23: else if (signit) /* signed ? */
24: {
25: if (value > 0) *sign = 0;
26: else
27: { value = -value;
28: *sign = 1;
29: }
30: c = (int)(value % radix);
31: value /= radix;
32: }
33: else /* unsigned */
34: { *sign = 0;
35: if (value < 0)
36: { /* ALL THIS IS TO SIMULATE UNSIGNED MOD & DIV */
37: kludge = mask - (radix - one);
38: value &= mask;
39: rem = (kludge % radix) + (value % radix);
40: value = (kludge / radix) + (value / radix)
41: + (rem / radix) + one;
42: c = (int)(rem % radix);
43: }
44: else
45: {
46: c = (int)(value % radix);
47: value /= radix;
48: }
49: }
50: *(buf+MAXINTLENGTH) = digit(c);
51: for(i=MAXINTLENGTH-one; value!=0; i--)
52: {
53: c = (int)(value % radix);
54: *(buf+i) = digit(c);
55: value /= radix;
56: }
57: *ndigit = MAXINTLENGTH - i;
58: return(&buf[i+one]);
59: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.