9

I am working on developing an embedded system (Cortex M3). For sending some data from the device to the serial port (to show on a PC screen), I use some own functions using putchar() method.

When I want to send integer or float, I use sprintf() in order to convert them to string of characters and sending them to the serial port.

Now, them problem is that I am using Keil uVision IDE and it is limited version with max 32 KB. Whenever I call sprintf() in different functions, I don't know why the size of the code after compile increased too much. I have surpassed 32 KB now and I wonder I have to change some of my functions and use something else instead of sprintf!

Any clue?

artless noise
  • 21,212
  • 6
  • 68
  • 105
Django
  • 159
  • 1
  • 3
  • 9
  • 7
    Try to avoid `float`s after all, as their runtime library components are quite big and slow in the most cases. – glglgl Oct 25 '12 at 08:43
  • 1
    the printf family is very costly as the large amount of code required to implement it, esp floating point. An integer only printf is a fair amount smaller but still costly. Do you really think you need printf() in your embedded microcontroller code? You can implement your own much cheaper solutions. – old_timer Oct 26 '12 at 18:01
  • Also look at your compiler vendors available libraries. Often there will be multiple flavor of the C std libs with functionality reduced. For example, an sprintf that handles int and float, but not scientific notation. – Josh Petitt Jan 31 '13 at 18:48

3 Answers3

9

Two potential offerings (neither of which I have used myself - my compiler vendors usually supply a stripped down printf for embedded use):

http://eprintf.sourceforge.net/ - [Sep 2017: unfortunately, seems to have gone away, but source code still here: https://sourceforge.net/projects/eprintf/files/ ]

http://www.sparetimelabs.com/tinyprintf/index.html - 2 files, about 1.4KB code. Option to enable 'longs' (means more code size). Supports leading zeros and field widths. No floating point support.

Martin Thompson
  • 16,395
  • 1
  • 38
  • 56
  • 1
    Since the `eprintf` webpage has no links to the actual code, here's a link to the sourceforge eprintf project: http://sourceforge.net/projects/eprintf/ – Michael Burr Oct 26 '12 at 05:32
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/17459102) – StoryTeller - Unslander Monica Sep 27 '17 at 09:28
  • @StoryTeller - ordinarily I'd agree, but I'm not sure a summary of the content helps if the links go away (as I see one of them has :( - when I'm just offering links to source code rather than a true "answer". – Martin Thompson Sep 27 '17 at 14:09
8

If you want it to be efficient, the best way is probably to code it yourself, or find some already written code for it on the net. Int to string conversion is however very simple, every programmer can write that in less than 30 minutes. Float to string conversion is a bit more intricate and depends on the floating point format used.

For convenience, here is a simple int-to-string algorithm for use in microcontroller applications:

void get_dec_str (uint8_t* str, size_t len, uint32_t val)
{
  uint8_t i;
  for(i=1; i<=len; i++)
  {
    str[len-i] = (uint8_t) ((val % 10UL) + '0');
    val/=10;
  }

  str[i-1] = '\0';
}
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 2
    I'm not sure that's a wildly quick/efficient function if your micro doesn't have a hardware divider... (or modulus :). Although I don't have a better option off the top of my head! – Martin Thompson Oct 25 '12 at 14:09
  • 3
    @MartinThompson I'm not sure there is much one can do about it, decimals are 10-based, but computers prefer 2/4/8/16/32-based. Besides I'd rather leave the optimization to the compiler. – Lundin Oct 25 '12 at 14:21
  • 1
    fair comment re: leaving the optimisation to the compiler – Martin Thompson Oct 25 '12 at 16:16
0

You can try itoa() or ftoa() and implement these as your requirement.I mean as they convert those to characters just inside the definition itself use putchar() to print directly.

This is should work , I think.

Omkant
  • 9,018
  • 8
  • 39
  • 59
  • 5
    There is no function called itoa nor one called ftoa in the C language. – Lundin Oct 25 '12 at 09:17
  • @Lundin : can be written as your requirement ...or include these files ...itoa and ftoa are in these headerfiles or – Omkant Oct 25 '12 at 09:21
  • there is no itoa() and ftoi() in C.(I dont know if it really exists in C++) – Django Oct 25 '12 at 09:24
  • @Omkant They are not in stdlib, since they are not part of any C or C++ standard. You are using some non-standard compiler. – Lundin Oct 25 '12 at 09:26
  • @Lundin : As i mentioned you can implement the same ... means write own definition – Omkant Oct 25 '12 at 09:29