source: firmware/FSC/src/num_conversion.c@ 12679

Last change on this file since 12679 was 11770, checked in by neise, 13 years ago
removed bug in nc_format. wrongly used pointer to piinter nc_format_buffer.
File size: 8.7 KB
Line 
1//-----------------------------------------------------------------------------
2
3#include "num_conversion.h"
4#include "math.h"
5//-----------------------------------------------------------------------------
6
7//__flash U08 NC_HEX_ARRAY[] = "0123456789ABCDEF";
8U08 NC_HEX_ARRAY[] = "0123456789ABCDEF";
9//-----------------------------------------------------------------------------
10
11U08 nc_buffer[NC_BUFFER_LENGTH]; // Conversion buffer
12U08 nc_format_buffer[NC_BUFFER_LENGTH]; // Format buffer
13
14static U08 nc_int_digits[8];
15static U08 nc_dec_digits[6];
16//-----------------------------------------------------------------------------
17
18pU08 nc_format(pU08 source_ptr,U08 digits)
19{
20 U08 len = strlen((const char *)source_ptr);
21 pU08 dest_ptr = (pU08)nc_format_buffer;
22
23 // Fillup loop
24 while (digits-- > len)
25 {
26 *dest_ptr++ = NC_FILL_CHAR;
27 }
28
29 // Copy loop
30 while (len--)
31 {
32 *dest_ptr++ = *source_ptr++;
33 }
34
35 *dest_ptr = 0; // Terminate format string
36
37 return (pU08)nc_format_buffer;
38}
39//-----------------------------------------------------------------------------
40
41pU08 nc_U08_to_str(U08 value,U08 digits)
42{
43 pU08 pstr = nc_buffer;
44
45 if (value >= 100) *pstr++ = ('0' + (value % 1000 / 100));
46 if (value >= 10) *pstr++ = ('0' + (value % 100 / 10));
47 *pstr++ = ('0' + (value % 10));
48 *pstr = 0;
49
50 if (!digits) // If digits = 0, then return buffer start
51 {
52 return nc_buffer;
53 }
54 else // Do formatted output
55 {
56 return nc_format(nc_buffer,digits);
57 }
58}
59//-----------------------------------------------------------------------------
60
61pU08 nc_S08_to_str(S08 signed_value,U08 digits)
62{
63 pU08 pstr = nc_buffer;
64 U08 value;
65
66 if (signed_value < 0)
67 {
68 *pstr++ = '-';
69 value = -signed_value;
70 }
71 else
72 {
73 value = signed_value;
74 }
75
76 if (value >= 100) *pstr++ = ('0' + (value % 1000 / 100));
77 if (value >= 10) *pstr++ = ('0' + (value % 100 / 10));
78 *pstr++ = ('0' + (value % 10));
79 *pstr = 0;
80
81 if (!digits) // If digits = 0, then return buffer start
82 {
83 return nc_buffer;
84 }
85 else // Do formatted output
86 {
87 return nc_format(nc_buffer,digits);
88 }
89}
90//-----------------------------------------------------------------------------
91
92pU08 nc_U08_to_hex(U08 value)
93{
94 pU08 pstr = nc_buffer;
95
96 *pstr++ = NC_HEX_ARRAY[value >> 4];
97 *pstr++ = NC_HEX_ARRAY[value & 0x0F];
98 *pstr = 0;
99
100 return (nc_buffer);
101}
102//-----------------------------------------------------------------------------
103
104pU08 nc_U08_to_bin(U08 value)
105{
106 pU08 pstr = nc_buffer;
107 U08 n;
108
109 for (n = 7; n < 8; n--)
110 {
111 if (value & (1 << n))
112 {
113 *pstr++ = '1';
114 }
115 else
116 {
117 *pstr++ = '0';
118 }
119 }
120
121 *pstr = 0;
122
123 return (nc_buffer);
124}
125//-----------------------------------------------------------------------------
126
127pU08 nc_U16_to_str(U16 value,U08 digits)
128{
129 pU08 pstr = nc_buffer;
130
131 if (value >= 10000) *pstr++ = ('0' + (value / 10000));
132 if (value >= 1000) *pstr++ = ('0' + (value % 10000 / 1000));
133 if (value >= 100) *pstr++ = ('0' + (value % 1000 / 100));
134 if (value >= 10)*pstr++ = ('0' + (value % 100 / 10));
135 *pstr++ = ('0' + (value % 10));
136 *pstr = 0;
137
138 if (!digits) // If digits = 0, then return buffer start
139 {
140 return nc_buffer;
141 }
142 else // Do formatted output
143 {
144 return nc_format(nc_buffer,digits);
145 }
146}
147//-----------------------------------------------------------------------------
148
149pU08 nc_S16_to_str(S16 signed_value,U08 digits)
150{
151 pU08 pstr = nc_buffer;
152 U16 value;
153
154 if (signed_value < 0)
155 {
156 *pstr++ = '-';
157 value = -signed_value;
158 }
159 else
160 {
161 value = signed_value;
162 }
163
164 if (value >= 10000) *pstr++ = ('0' + (value / 10000));
165 if (value >= 1000) *pstr++ = ('0' + (value % 10000 / 1000));
166 if (value >= 100) *pstr++ = ('0' + (value % 1000 / 100));
167 if (value >= 10) *pstr++ = ('0' + (value % 100 / 10));
168 *pstr++ = ('0' + (value % 10));
169 *pstr = 0;
170
171 if (!digits) // If digits = 0, then return buffer start
172 {
173 return nc_buffer;
174 }
175 else // Do formatted output
176 {
177 return nc_format(nc_buffer,digits);
178 }
179}
180//-----------------------------------------------------------------------------
181
182pU08 nc_U16_to_hex(U16 value)
183{
184 pU08 pstr = nc_buffer;
185 tMEM16 data;
186
187 data.word = value;
188
189 *pstr++ = NC_HEX_ARRAY[(data.byte[1]) >> 4];
190 *pstr++ = NC_HEX_ARRAY[(data.byte[1]) & 0x0F];
191
192 *pstr++ = NC_HEX_ARRAY[(data.byte[0]) >> 4];
193 *pstr++ = NC_HEX_ARRAY[(data.byte[0]) & 0x0F];
194
195 *pstr = 0;
196
197 return (nc_buffer);
198}
199//-----------------------------------------------------------------------------
200
201pU08 nc_U32_to_str(U32 value,U08 digits)
202{
203 pU08 pstr = nc_buffer;
204
205 if (value >= 1000000000) *pstr++ = ('0' + (value / 1000000000));
206 if (value >= 100000000) *pstr++ = ('0' + (value % 1000000000 / 100000000));
207 if (value >= 10000000) *pstr++ = ('0' + (value % 100000000 / 10000000));
208 if (value >= 1000000) *pstr++ = ('0' + (value % 10000000 / 1000000));
209 if (value >= 100000) *pstr++ = ('0' + (value % 1000000 / 100000));
210 if (value >= 10000) *pstr++ = ('0' + (value % 100000 / 10000));
211 if (value >= 1000) *pstr++ = ('0' + (value % 10000 / 1000));
212 if (value >= 100) *pstr++ = ('0' + (value % 1000 / 100));
213 if (value >= 10) *pstr++ = ('0' + (value % 100 / 10));
214
215 *pstr++ = ('0' + (value % 10));
216 *pstr = 0;
217
218 if (!digits) // If digits = 0, then return buffer start
219 {
220 return nc_buffer;
221 }
222 else // Do formatted output
223 {
224 return nc_format(nc_buffer,digits);
225 }
226}
227//-----------------------------------------------------------------------------
228
229pU08 nc_S32_to_str(S32 signed_value,U08 digits)
230{
231 pU08 pstr = nc_buffer;
232 U32 value;
233
234 if (signed_value < 0)
235 {
236 *pstr++ = '-';
237 value = -signed_value;
238 }
239 else
240 {
241 value = signed_value;
242 }
243
244 if (value >= 1000000000) *pstr++ = ('0' + (value / 1000000000));
245 if (value >= 100000000) *pstr++ = ('0' + (value % 1000000000 / 100000000));
246 if (value >= 10000000) *pstr++ = ('0' + (value % 100000000 / 10000000));
247 if (value >= 1000000) *pstr++ = ('0' + (value % 10000000 / 1000000));
248 if (value >= 100000) *pstr++ = ('0' + (value % 1000000 / 100000));
249 if (value >= 10000) *pstr++ = ('0' + (value % 100000 / 10000));
250 if (value >= 1000) *pstr++ = ('0' + (value % 10000 / 1000));
251 if (value >= 100) *pstr++ = ('0' + (value % 1000 / 100));
252 if (value >= 10) *pstr++ = ('0' + (value % 100 / 10));
253
254 *pstr++ = ('0' + (value % 10));
255 *pstr = 0;
256
257 if (!digits) // If digits = 0, then return buffer start
258 {
259 return nc_buffer;
260 }
261 else // Do formatted output
262 {
263 return nc_format(nc_buffer,digits);
264 }
265}
266//-----------------------------------------------------------------------------
267
268pU08 nc_U32_to_hex(U32 value)
269{
270 pU08 pstr = nc_buffer;
271 tMEM32 data;
272
273 data.dword = value;
274
275 *pstr++ = NC_HEX_ARRAY[(data.byte[3]) >> 4];
276 *pstr++ = NC_HEX_ARRAY[(data.byte[3]) & 0x0F];
277
278 *pstr++ = NC_HEX_ARRAY[(data.byte[2]) >> 4];
279 *pstr++ = NC_HEX_ARRAY[(data.byte[2]) & 0x0F];
280
281 *pstr++ = NC_HEX_ARRAY[(data.byte[1]) >> 4];
282 *pstr++ = NC_HEX_ARRAY[(data.byte[1]) & 0x0F];
283
284 *pstr++ = NC_HEX_ARRAY[(data.byte[0]) >> 4];
285 *pstr++ = NC_HEX_ARRAY[(data.byte[0]) & 0x0F];
286
287 *pstr = 0;
288
289 return (nc_buffer);
290}
291//-----------------------------------------------------------------------------
292
293pU08 nc_float_to_str(float value,U08 decimals,U08 digits)
294{
295 S08 n;
296 U08 int_count;
297 U08 dec_count;
298 float dec_part;
299 U32 int_part;
300 pU08 pstr = nc_buffer;
301
302 if (decimals > 5)
303 {
304 decimals = 5;
305 }
306
307 if (isnan(value)){
308 pstr[0]=pstr[2]='n';
309 pstr[1]='a';
310 pstr[3]=0;
311 return nc_buffer;
312 }
313 if (value < 0.0)
314 {
315 *pstr++ = '-';
316 value = -value;
317 }
318
319 int_part = value;
320 dec_part = value - int_part;
321 int_count = 0;
322 dec_count = 0;
323
324 if (int_part == 0)
325 {
326 *pstr++ = '0';
327 }
328
329 while (int_part > 0)
330 {
331 nc_int_digits[int_count] = int_part % 10;
332 int_part = int_part - nc_int_digits[int_count];
333
334 if (int_part > 0)
335 {
336 int_part = int_part / 10;
337 }
338
339 int_count++; // go to the next digit
340 }
341
342 while (dec_count < decimals)
343 {
344 dec_part = dec_part * 10.0;
345 nc_dec_digits[dec_count] = dec_part;
346
347 if (nc_dec_digits[dec_count] > 0)
348 {
349 dec_part = dec_part - nc_dec_digits[dec_count];
350 }
351
352 dec_count++;
353 }
354
355 for (n = int_count - 1; n > -1; n--)
356 {
357 *pstr++ = 48 + nc_int_digits[n];
358 }
359
360 *pstr++ = '.';
361
362 for (n = 0; n < dec_count; n++)
363 {
364 *pstr++ = 48 + nc_dec_digits[n];
365 }
366
367 // Terminate string
368 *pstr = 0;
369
370 if (!digits) // If digits = 0, then return buffer start
371 {
372 return nc_buffer;
373 }
374 else // Do formatted output
375 {
376 return nc_format(nc_buffer,digits);
377 }
378}
Note: See TracBrowser for help on using the repository browser.