Replace sprintf

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Replace sprintf

5,395 次查看
jarin
Contributor I
Hi all,
in my application (I'm using CW3.0 for HC08) I would like to convert int variable into string, to output it on display.
So I placed sprintf function, like that:

int int_var;
char lcd_buffer[86];
...
...
...
sprintf(lcd_buffer,"value is: %i",int_var)
...
...

It worked fine, of course, but sprintf function occupied about 6kbytes of flash memory (!!!). Is there a way to shrink it? Or some other function to convert int to string? I know, that sprintf is able to convert float, but it's useless for me, just eating expensive flash memory (I need to convert just integer numbers, like int or char).

Thank you for your help.

Jarin
标签 (1)
标记 (1)
0 项奖励
6 回复数

638 次查看
Lundin
Senior Contributor IV
This is one of the reasons why it isn't wise to use sprintf() in embedded/real time applications. My advice is to avoid all library functions and write your own versions of them.
0 项奖励

638 次查看
jarin
Contributor I
Lundin, I know, it's better to write my own functions to handle this... but I hope somebody have also those function public, to inspire me :smileyhappy:
0 项奖励

638 次查看
CrasyCat
Specialist III

Hello

Are you intending to use floating point variables in printf?
You should be able to get a smaller footprint for that function if you are not using floating point.

Upgrading to a later version of CodeWarrior (V3.1 or later)), we have new library files used when floating point arithmetic is used in the application (ansii.lib instead of ansi.lib) for instance.

Footprint for sprintf is much smaller there (around 1600 bytes if I remember well).
If you want to stick with V3.0  you may have to regenerate the desired ANSI library specifying you do not want to use floating point arithmetic in sprintf.

Take a look at the attached technical note for information on how to regenerate library files.

You have to set  LIBDEF_PRINTF_FLOATING to 0 in libdefs.h and rebuild the library (or at least rebuild printf.c).

If you have a special edition I think you will not be able to rebuild the whole library. You will then have to rebuild printf only

I hope this helps.

CrasyCat

0 项奖励

638 次查看
jarin
Contributor I
CrasyCat:
Yes, it looks very good. I complained about size of sprintf function, because I don't need floating point suuport in it.
I'll try it, because 1,6KB is much better compared to about 6KB, thank you very much or your help.

Jarin
0 项奖励

638 次查看
Lundin
Senior Contributor IV
Seriously... 1600 bytes for ASCII to int convertion...
It is maybe acceptable in some hobby project. Maybe.

A snippet doing that converstion would take around 100 bytes, depending on how code-effective the mcu is.
I could post the code for one, but if you don't know how to write it yourself... well...
0 项奖励

638 次查看
CrasyCat
Specialist III

Hello

Well printf is not just converting an int to a string. It is doing much more that this (specifically in formatting the output string).

The size specified is the size of the function sprintf which does not support floating point conversion.

For sure if you just need to convert a int to a string, you can get a smaller footprint.

CrasyCat

0 项奖励