|
| 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 | +//**************************************************************************** |
| 20 | +// @Project Includes |
| 21 | +//**************************************************************************** |
| 22 | +#include "api/itoa.h" |
| 23 | +#include <string.h> |
| 24 | + |
| 25 | +//**************************************************************************** |
| 26 | +// @Local Functions |
| 27 | +//**************************************************************************** |
| 28 | + |
| 29 | +#ifdef __cplusplus |
| 30 | +extern "C" { |
| 31 | +#endif // __cplusplus |
| 32 | + |
| 33 | +#if 0 |
| 34 | + /* reverse: reverse string s in place */ |
| 35 | + static void reverse( char s[] ) |
| 36 | + { |
| 37 | + int i, j ; |
| 38 | + char c ; |
| 39 | + |
| 40 | + for ( i = 0, j = strlen(s) - 1 ; i < j ; i++, j-- ) |
| 41 | + { |
| 42 | + c = s[i] ; |
| 43 | + s[i] = s[j] ; |
| 44 | + s[j] = c ; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /* itoa: convert n to characters in s */ |
| 49 | + extern void itoa( int n, char s[] ) |
| 50 | + { |
| 51 | + int i, sign ; |
| 52 | + |
| 53 | + if ( (sign = n) < 0 ) /* record sign */ |
| 54 | + { |
| 55 | + n = -n; /* make n positive */ |
| 56 | + } |
| 57 | + |
| 58 | + i = 0; |
| 59 | + do |
| 60 | + { |
| 61 | + /* generate digits in reverse order */ |
| 62 | + s[i++] = n % 10 + '0'; /* get next digit */ |
| 63 | + } |
| 64 | + while ((n /= 10) > 0) ; /* delete it */ |
| 65 | + |
| 66 | + if (sign < 0 ) |
| 67 | + { |
| 68 | + s[i++] = '-'; |
| 69 | + } |
| 70 | + |
| 71 | + s[i] = '\0'; |
| 72 | + |
| 73 | + reverse( s ) ; |
| 74 | + } |
| 75 | + |
| 76 | +#else |
| 77 | + |
| 78 | +extern char *itoa(int value, char *string, int radix) { return ltoa(value, string, radix); } |
| 79 | + |
| 80 | +extern char *ltoa(long value, char *string, int radix) { |
| 81 | + char tmp[33]; |
| 82 | + char *tp = tmp; |
| 83 | + long i; |
| 84 | + unsigned long v; |
| 85 | + int sign; |
| 86 | + char *sp; |
| 87 | + |
| 88 | + if (string == NULL) { |
| 89 | + return 0; |
| 90 | + } |
| 91 | + |
| 92 | + if (radix > 36 || radix <= 1) { |
| 93 | + return 0; |
| 94 | + } |
| 95 | + |
| 96 | + sign = (radix == 10 && value < 0); |
| 97 | + if (sign) { |
| 98 | + v = -value; |
| 99 | + } else { |
| 100 | + v = (unsigned long)value; |
| 101 | + } |
| 102 | + |
| 103 | + while (v || tp == tmp) { |
| 104 | + i = v % radix; |
| 105 | + v = v / radix; |
| 106 | + if (i < 10) { |
| 107 | + *tp++ = i + '0'; |
| 108 | + } else { |
| 109 | + *tp++ = i + 'a' - 10; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + sp = string; |
| 114 | + |
| 115 | + if (sign) { |
| 116 | + *sp++ = '-'; |
| 117 | + } |
| 118 | + while (tp > tmp) { |
| 119 | + *sp++ = *--tp; |
| 120 | + } |
| 121 | + *sp = 0; |
| 122 | + |
| 123 | + return string; |
| 124 | +} |
| 125 | + |
| 126 | +extern char *utoa(unsigned int value, char *string, int radix) { |
| 127 | + return ultoa(value, string, radix); |
| 128 | +} |
| 129 | + |
| 130 | +extern char *ultoa(unsigned long value, char *string, int radix) { |
| 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 | + return 0; |
| 139 | + } |
| 140 | + |
| 141 | + if (radix > 36 || radix <= 1) { |
| 142 | + return 0; |
| 143 | + } |
| 144 | + |
| 145 | + while (v || tp == tmp) { |
| 146 | + i = v % radix; |
| 147 | + v = v / radix; |
| 148 | + if (i < 10) { |
| 149 | + *tp++ = i + '0'; |
| 150 | + } else { |
| 151 | + *tp++ = i + 'a' - 10; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + sp = string; |
| 156 | + |
| 157 | + while (tp > tmp) { |
| 158 | + *sp++ = *--tp; |
| 159 | + } |
| 160 | + *sp = 0; |
| 161 | + |
| 162 | + return string; |
| 163 | +} |
| 164 | +#endif /* 0 */ |
| 165 | + |
| 166 | +#ifdef __cplusplus |
| 167 | +} // extern "C" |
| 168 | +#endif // __cplusplus |
| 169 | + |
| 170 | +//**************************************************************************** |
| 171 | +// END OF FILE |
| 172 | +//**************************************************************************** |
| 173 | + |
0 commit comments