0

I am using PIC18F2550. Programming it with C18 language.
I need a function that converts double to string like below:

void dtoa(  char *szString,             // Output string
            double dbDouble,            // Input number
            unsigned char ucFPlaces)    // Number of digits in the resulting fractional part
{
    // ??????????????
}

To be called like this in the main program:

void main (void)
{
    // ...
    double dbNumber = 123.45678;
    char szText[9];
    dtoa(szText, dbNumber, 3); // szText becomes "123.456" or rounded to "123.457"
    // ...
}
5
  • sprintf() then find the '.' and null the right decimal character? Commented Feb 7, 2012 at 21:44
  • @kenny sprintf() is possibly a bit optomistic on a PIC Commented Feb 7, 2012 at 21:46
  • @MartinBeckett I'm sure it's available if you have the space. Or roll your own dtoa() Commented Feb 7, 2012 at 21:52
  • 1
    "Available" can be a nuanced question since standards like MISRA ban the use of variable arguments... Commented Feb 7, 2012 at 22:02
  • I use sprintf all the time on the PIC18, and doubles as well. Commented Apr 5, 2012 at 15:41

3 Answers 3

1

So write one!

5mins, a bit of graph paper and a coffee is all it should take.
In fact it's a good interview question

Sign up to request clarification or add additional context in comments.

Comments

1

Generally, the Newlib C library (BSD license, from RedHat, part of Cygwin as well as used in many many "bare-metal" embedded-systems compilers) is a good place to start for usefuls sources for things that would be in the standard C library.

The Newlib dtoa.c sources are in the src/newlib/libc/stdlib subdirectory of the source tree:

The file is going to be a little odd, in that Newlib uses some odd macros for the function declarations, but should be straightforward to adapt -- and, being BSD-licensed, you can pretty much do whatever you want with it if you keep the copyright notice on it.

Comments

0

Tiny printf might work for you: http://www.sparetimelabs.com/tinyprintf/index.html

2 Comments

As mentioned on that page: "Of course being tiny, it has limitations, no floating point support..."
Ah yes, good catch. Now I dimly recall rolling in our own floating point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.