|
| 1 | +/* |
| 2 | + Copyright (c) 2014 Arduino LLC. 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 <../core-api/api/itoa.h> |
| 20 | +#include <../core-api/api/deprecated-avr-comp/avr/dtostrf.c.impl> |
| 21 | +#include <string.h> |
| 22 | + |
| 23 | +#ifdef __cplusplus |
| 24 | +extern "C" { |
| 25 | +#endif |
| 26 | + |
| 27 | +extern char* itoa( int value, char *string, int radix ) |
| 28 | +{ |
| 29 | + return ltoa( value, string, radix ) ; |
| 30 | +} |
| 31 | + |
| 32 | +extern char* ltoa( long value, char *string, int radix ) |
| 33 | +{ |
| 34 | + char tmp[33]; |
| 35 | + char *tp = tmp; |
| 36 | + long i; |
| 37 | + unsigned long v; |
| 38 | + int sign; |
| 39 | + char *sp; |
| 40 | + |
| 41 | + if ( string == NULL ) |
| 42 | + { |
| 43 | + return 0 ; |
| 44 | + } |
| 45 | + |
| 46 | + if (radix > 36 || radix <= 1) |
| 47 | + { |
| 48 | + return 0 ; |
| 49 | + } |
| 50 | + |
| 51 | + sign = (radix == 10 && value < 0); |
| 52 | + if (sign) |
| 53 | + { |
| 54 | + v = -value; |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + v = (unsigned long)value; |
| 59 | + } |
| 60 | + |
| 61 | + while (v || tp == tmp) |
| 62 | + { |
| 63 | + i = v % radix; |
| 64 | + v = v / radix; |
| 65 | + if (i < 10) |
| 66 | + *tp++ = i+'0'; |
| 67 | + else |
| 68 | + *tp++ = i + 'a' - 10; |
| 69 | + } |
| 70 | + |
| 71 | + sp = string; |
| 72 | + |
| 73 | + if (sign) |
| 74 | + *sp++ = '-'; |
| 75 | + while (tp > tmp) |
| 76 | + *sp++ = *--tp; |
| 77 | + *sp = 0; |
| 78 | + |
| 79 | + return string; |
| 80 | +} |
| 81 | + |
| 82 | +extern char* utoa( unsigned int value, char *string, int radix ) |
| 83 | +{ |
| 84 | + return ultoa( value, string, radix ) ; |
| 85 | +} |
| 86 | + |
| 87 | +extern char* ultoa( unsigned long value, char *string, int radix ) |
| 88 | +{ |
| 89 | + char tmp[33]; |
| 90 | + char *tp = tmp; |
| 91 | + long i; |
| 92 | + unsigned long v = value; |
| 93 | + char *sp; |
| 94 | + |
| 95 | + if ( string == NULL ) |
| 96 | + { |
| 97 | + return 0; |
| 98 | + } |
| 99 | + |
| 100 | + if (radix > 36 || radix <= 1) |
| 101 | + { |
| 102 | + return 0; |
| 103 | + } |
| 104 | + |
| 105 | + while (v || tp == tmp) |
| 106 | + { |
| 107 | + i = v % radix; |
| 108 | + v = v / radix; |
| 109 | + if (i < 10) |
| 110 | + *tp++ = i+'0'; |
| 111 | + else |
| 112 | + *tp++ = i + 'a' - 10; |
| 113 | + } |
| 114 | + |
| 115 | + sp = string; |
| 116 | + |
| 117 | + |
| 118 | + while (tp > tmp) |
| 119 | + *sp++ = *--tp; |
| 120 | + *sp = 0; |
| 121 | + |
| 122 | + return string; |
| 123 | +} |
| 124 | + |
| 125 | +#ifdef __cplusplus |
| 126 | +} // extern "C" |
| 127 | +#endif |
0 commit comments