Imported Upstream version 2.9.0
[debian/cc1111] / 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 void _uitoa(unsigned int value, char* string, unsigned char radix)
17 {
18   signed char index = 0, i = 0;
19
20   /* generate the number in reverse order */
21   do {
22     string[index] = '0' + (value % radix);
23     if (string[index] > '9')
24         string[index] += 'A' - '9' - 1;
25     value /= radix;
26     ++index;
27   } while (value != 0);
28
29   /* null terminate the string */
30   string[index--] = '\0';
31
32   /* reverse the order of digits */
33   while (index > i) {
34     char tmp = string[i];
35     string[i] = string[index];
36     string[index] = tmp;
37     ++i;
38     --index;
39   }
40 }
41
42 void _itoa(int value, char* string, unsigned char radix)
43 {
44   if (value < 0 && radix == 10) {
45     *string++ = '-';
46     value = -value;
47   }
48   _uitoa(value, string, radix);
49 }