Skip to content
This repository was archived by the owner on Apr 16, 2021. It is now read-only.

Commit 0573071

Browse files
committed
Add iota/ltoa implementation
Borrowed from SAMD core; fixes #6
1 parent 7c86454 commit 0573071

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

cores/arduino/itoa.c

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

0 commit comments

Comments
 (0)