Andes Workshop
http://forum.andestech.com/

Simply Expand Sprintf() in MCUlib
http://forum.andestech.com/viewtopic.php?f=16&t=898
Page 1 of 1

Author:  HuJin [ Fri Oct 24, 2014 10:25 am ]
Post subject:  Simply Expand Sprintf() in MCUlib

There is known limitation of MCUlib's sprintf() that it can't support "05.2f" format (refer 《Andes Programming guide》 Chart "Andes MCUlib" ) while newlib support this well. If customers want this feature of sprintf() and also want to choose mculib because benefits of codesize, following is a simply way to implement it.

We give an function which can pad zero to original sprintf()'s output. The prototype is:
char *float2str(char *out_str, int len, char *format, double f);
(len must be specify by user)

Follow is an example which used this function:
Code:
double d1 = sin(1 + 0.1);
double d2 = sin(1 + 0.3);
unsigned int i1 = 12345678;
unsigned int i2 = 9876345;
char buf1[300];
char temp1[20];
char temp2[20];
sprintf (buf1,"/%d %2d %s /%s\r\n", i1,i2, float2str(temp1, 8, "%05.2f", d1),float2str(temp2, 13, "%08.4f", d2));
printf("string is %s\n",buf1);
The result is:
Attachment:
test1.png
test1.png [ 1.17 KiB | Viewed 5718 times ]


The source code & the example demo can be found here:
Attachment:
float2str.c [347 Bytes]
Downloaded 487 times

Attachment:
demo-sprintf.tar [610 KiB]
Downloaded 353 times

Page 1 of 1 All times are UTC + 8 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/