1 | /*
|
---|
2 | Copyright (c) 2011 Arduino. All right reserved.
|
---|
3 |
|
---|
4 | This library is free software; you can redistribute it and/or
|
---|
5 | modify it under the terms of the GNU Lesser General Public
|
---|
6 | License as published by the Free Software Foundation; either
|
---|
7 | version 2.1 of the License, or (at your option) any later version.
|
---|
8 |
|
---|
9 | This library is distributed in the hope that it will be useful,
|
---|
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
---|
12 | See the GNU Lesser General Public License for more details.
|
---|
13 |
|
---|
14 | You should have received a copy of the GNU Lesser General Public
|
---|
15 | License along with this library; if not, write to the Free Software
|
---|
16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
17 | */
|
---|
18 |
|
---|
19 | #include "itoa.h"
|
---|
20 | #include <string.h>
|
---|
21 |
|
---|
22 | #ifdef __cplusplus
|
---|
23 | extern "C"{
|
---|
24 | #endif // __cplusplus
|
---|
25 |
|
---|
26 | #if 0
|
---|
27 | /* reverse: reverse string s in place */
|
---|
28 | static void reverse( char s[] )
|
---|
29 | {
|
---|
30 | int i, j ;
|
---|
31 | char c ;
|
---|
32 |
|
---|
33 | for ( i = 0, j = strlen(s)-1 ; i < j ; i++, j-- )
|
---|
34 | {
|
---|
35 | c = s[i] ;
|
---|
36 | s[i] = s[j] ;
|
---|
37 | s[j] = c ;
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | /* itoa: convert n to characters in s */
|
---|
42 | extern void itoa( int n, char s[] )
|
---|
43 | {
|
---|
44 | int i, sign ;
|
---|
45 |
|
---|
46 | if ( (sign = n) < 0 ) /* record sign */
|
---|
47 | {
|
---|
48 | n = -n; /* make n positive */
|
---|
49 | }
|
---|
50 |
|
---|
51 | i = 0;
|
---|
52 | do
|
---|
53 | { /* generate digits in reverse order */
|
---|
54 | s[i++] = n % 10 + '0'; /* get next digit */
|
---|
55 | } while ((n /= 10) > 0) ; /* delete it */
|
---|
56 |
|
---|
57 | if (sign < 0 )
|
---|
58 | {
|
---|
59 | s[i++] = '-';
|
---|
60 | }
|
---|
61 |
|
---|
62 | s[i] = '\0';
|
---|
63 |
|
---|
64 | reverse( s ) ;
|
---|
65 | }
|
---|
66 |
|
---|
67 | #else
|
---|
68 |
|
---|
69 | extern char* itoa( int value, char *string, int radix )
|
---|
70 | {
|
---|
71 | return ltoa( value, string, radix ) ;
|
---|
72 | }
|
---|
73 |
|
---|
74 | extern char* ltoa( long value, char *string, int radix )
|
---|
75 | {
|
---|
76 | char tmp[33];
|
---|
77 | char *tp = tmp;
|
---|
78 | long i;
|
---|
79 | unsigned long v;
|
---|
80 | int sign;
|
---|
81 | char *sp;
|
---|
82 |
|
---|
83 | if ( string == NULL )
|
---|
84 | {
|
---|
85 | return 0 ;
|
---|
86 | }
|
---|
87 |
|
---|
88 | if (radix > 36 || radix <= 1)
|
---|
89 | {
|
---|
90 | return 0 ;
|
---|
91 | }
|
---|
92 |
|
---|
93 | sign = (radix == 10 && value < 0);
|
---|
94 | if (sign)
|
---|
95 | {
|
---|
96 | v = -value;
|
---|
97 | }
|
---|
98 | else
|
---|
99 | {
|
---|
100 | v = (unsigned long)value;
|
---|
101 | }
|
---|
102 |
|
---|
103 | while (v || tp == tmp)
|
---|
104 | {
|
---|
105 | i = v % radix;
|
---|
106 | v = v / radix;
|
---|
107 | if (i < 10)
|
---|
108 | *tp++ = i+'0';
|
---|
109 | else
|
---|
110 | *tp++ = i + 'a' - 10;
|
---|
111 | }
|
---|
112 |
|
---|
113 | sp = string;
|
---|
114 |
|
---|
115 | if (sign)
|
---|
116 | *sp++ = '-';
|
---|
117 | while (tp > tmp)
|
---|
118 | *sp++ = *--tp;
|
---|
119 | *sp = 0;
|
---|
120 |
|
---|
121 | return string;
|
---|
122 | }
|
---|
123 |
|
---|
124 | extern char* utoa( unsigned long value, char *string, int radix )
|
---|
125 | {
|
---|
126 | return ultoa( value, string, radix ) ;
|
---|
127 | }
|
---|
128 |
|
---|
129 | extern char* ultoa( unsigned long value, char *string, int radix )
|
---|
130 | {
|
---|
131 | char tmp[33];
|
---|
132 | char *tp = tmp;
|
---|
133 | long i;
|
---|
134 | unsigned long v = value;
|
---|
135 | char *sp;
|
---|
136 |
|
---|
137 | if ( string == NULL )
|
---|
138 | {
|
---|
139 | return 0;
|
---|
140 | }
|
---|
141 |
|
---|
142 | if (radix > 36 || radix <= 1)
|
---|
143 | {
|
---|
144 | return 0;
|
---|
145 | }
|
---|
146 |
|
---|
147 | while (v || tp == tmp)
|
---|
148 | {
|
---|
149 | i = v % radix;
|
---|
150 | v = v / radix;
|
---|
151 | if (i < 10)
|
---|
152 | *tp++ = i+'0';
|
---|
153 | else
|
---|
154 | *tp++ = i + 'a' - 10;
|
---|
155 | }
|
---|
156 |
|
---|
157 | sp = string;
|
---|
158 |
|
---|
159 |
|
---|
160 | while (tp > tmp)
|
---|
161 | *sp++ = *--tp;
|
---|
162 | *sp = 0;
|
---|
163 |
|
---|
164 | return string;
|
---|
165 | }
|
---|
166 | #endif /* 0 */
|
---|
167 |
|
---|
168 | #ifdef __cplusplus
|
---|
169 | } // extern "C"
|
---|
170 | #endif // __cplusplus
|
---|