6 * @purpose Implement sprintf functions, because WinAVR can't do it
\r
9 //#include <math.h> // Needed for modf
\r
11 int luint2str(char * str, uint32 integer)
\r
25 while (integer >= base)
\r
27 remainder = integer % base;
\r
28 integer = integer / base;
\r
29 str[i] = (char)((uint)'0' + remainder);
\r
35 str[i] = (char)((uint)'0' + integer);
\r
39 //reverse the string
\r
40 for (int j = 0; j < i/2; ++j)
\r
43 str[j] = str[i - 1 - j];
\r
52 int uint2str(char * str, uint integer)
\r
66 while (integer >= base)
\r
68 remainder = integer % base;
\r
69 integer = integer / base;
\r
70 str[i] = (char)((uint)'0' + remainder);
\r
76 str[i] = (char)((uint)'0' + integer);
\r
80 //reverse the string
\r
81 for (int j = 0; j < i/2; ++j)
\r
84 str[j] = str[i - 1 - j];
\r
93 int int2str(char * str, int integer)
\r
104 char negative = FALSE;
\r
111 while (integer >= base)
\r
113 remainder = integer % base;
\r
114 integer = integer / base;
\r
115 str[i] = (char)((uint)'0' + remainder);
\r
121 str[i] = (char)((uint)'0' + integer);
\r
125 if (negative == TRUE)
\r
132 //reverse the string
\r
133 for (int j = 0; j < i/2; ++j)
\r
136 str[j] = str[i - 1 - j];
\r
145 int float2str(char * str, float flt)
\r
149 int intpart = (int)(flt);
\r
150 double decpart = flt - (double)(intpart);
\r
151 int s = int2str(str, intpart);
\r
155 s += int2str(str+s, (int)(decpart*places));
\r
160 int str2str(char * str1, char * str2)
\r
163 for (i = 0; str2[i] != '\0'; ++i)
\r
171 int vsprintf(char * str, const char * format, va_list args)
\r
175 for (f = 0; format[f] != '\0'; ++f)
\r
177 if (format[f] == '%')
\r
179 switch (format[f+1])
\r
185 uint integer = va_arg(args, uint);
\r
186 s += uint2str(str+s, integer);
\r
191 uint32 integer = va_arg(args, uint32);
\r
192 s += luint2str(str+s, integer);
\r
197 char * str2 = va_arg(args, char*);
\r
198 s += str2str(str+s, str2);
\r
204 float flt = va_arg(args, double);
\r
205 s += float2str(str+s, flt);
\r
209 str[s] = (char)(va_arg(args, int));
\r
220 str[s] = format[f];
\r
228 int sprintf(char * str, const char * format, ...)
\r
232 va_start(args, format);
\r
233 result = vsprintf(str, format, args);
\r