Andes Workshop

Simply Expand Sprintf() in MCUlib
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:
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:
test1.png [ 1.17 KiB | Viewed 7799 times ]

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

demo-sprintf.tar [610 KiB]
Downloaded 511 times

Page 1 of 1 All times are UTC + 8 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group