Andes Workshop

It is currently Tue Mar 19, 2024 2:21 pm

All times are UTC + 8 hours [ DST ]




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Simply Expand Sprintf() in MCUlib
PostPosted: Fri Oct 24, 2014 10:25 am 
Offline

Joined: Fri May 04, 2012 6:49 am
Posts: 17
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 11777 times ]


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

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


Top
 Profile Send private message E-mail  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC + 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group