* Removed svn:executable property from non-executable files
[fw/sdcc] / device / lib / _itoa.c
1 /*-------------------------------------------------------------------------
2  integer to string conversion
3
4  Written by:   Bela Torok, 1999
5                bela.torok@kssg.ch
6  usage:
7
8  _uitoa(unsigned int value, char* string, int radix)
9  _itoa(int 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 16
17
18 void _uitoa(unsigned int value, char* string, unsigned char radix)
19 {
20 unsigned char index, i;
21 /* char buffer[NUMBER_OF_DIGITS]; */ /* space for NUMBER_OF_DIGITS + '\0' */
22
23   index = NUMBER_OF_DIGITS;
24   i = 0;
25
26   do {
27     string[--index] = '0' + (value % radix);
28     if ( string[index] > '9') string[index] += 'A' - '9' - 1;
29     value /= radix;
30   } while (value != 0);
31
32   do {
33     string[i++] = string[index++];
34   } while ( index < NUMBER_OF_DIGITS );
35
36   string[i] = 0; /* string terminator */
37 }
38
39 void _itoa(int value, char* string, unsigned char radix)
40 {
41   if (value < 0 && radix == 10) {
42     *string++ = '-';
43     value = -value;
44   }
45   _uitoa(value, string, radix);
46 }