Imported Upstream version 2.9.0
[debian/cc1111] / device / lib / pic16 / libc / stdlib / ltoa.c
1 /*-------------------------------------------------------------------------
2  integer to string conversion
3
4  Written by:   Bela Torok, 1999
5                bela.torok@kssg.ch
6  usage:
7
8  _ultoa(unsigned long value, char* string, int radix)
9  _ltoa(long value, char* string, int radix)
10
11  value  ->  Number to be converted
12  string ->  Result
13  radix  ->  Base of value (e.g.: 2 for binary, 10 for decimal, 16 for hex)
14 ---------------------------------------------------------------------------*/
15
16 #define NUMBER_OF_DIGITS 32
17
18 #if _DEBUG
19 extern void io_long(unsigned long);
20 extern void io_str(char *);
21 #endif
22
23
24 void ultoa(unsigned long value, __data char* str, unsigned char radix)
25 {
26   unsigned int index;
27   unsigned char ch;
28   unsigned char buffer[NUMBER_OF_DIGITS];  /* space for NUMBER_OF_DIGITS + '\0' */
29
30     index = NUMBER_OF_DIGITS;
31
32     do {
33       ch = '0' + (value % radix);
34       if ( ch > '9') ch += 'a' - '9' - 1;
35
36 #if _DEBUG
37       io_str( "ultoa: " );
38       io_long( value );
39       io_long( (unsigned long) ch );
40 #endif
41
42       buffer[ --index ] = ch;
43       value /= radix;
44     } while (value != 0);
45
46     do {
47       *str++ = buffer[index++];
48     } while ( index < NUMBER_OF_DIGITS );
49
50     *str = 0;  /* string terminator */
51 }
52
53 void ltoa(long value, __data char* str, unsigned char radix)
54 {
55 #if _DEBUG
56   io_str( "ltoa: " );
57   io_long( (unsigned long)value );
58 #endif
59
60   if (value < 0 && radix == 10) {
61     *str++ = '-';
62     value = -value;
63   }
64
65
66
67   ultoa((unsigned long)value, str, radix);
68 }