zapta
Posts: 52
Joined: Thu May 06, 2021 4:23 pm

How to convert a double to a C string?

Tue May 11, 2021 3:24 pm

I am trying to convert a double to a C string. Any suggestion on how this can be done? Preferably without anything that will allocate memory from the heap. I looked at the SDK floating point functions bout couldn't find any string conversion.

E.g.

double x = -12.3.
char bfr[50]; // Assuming large enough.

double_to_string(x, 2, bfr); // value, after decimal point, buffer address, buffer size.

expecting bfr to have "-12.30\0";

cleverca22
Posts: 3753
Joined: Sat Aug 18, 2012 2:33 pm

Re: How to convert a double to a C string?

Tue May 11, 2021 3:31 pm

snprintf is the usual way to do that


User avatar
jahboater
Posts: 7041
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: How to convert a double to a C string?

Tue May 11, 2021 3:39 pm

snprintf(bfr, 50, "%.2f", x);

or

sprintf(bfr, "%.2f", x);

which wont check that the buffer is large enough (but the C compiler may do so instead).

On the Pi you can say "man sprintf" to display the documentation.

carlk3
Posts: 57
Joined: Wed Feb 17, 2021 8:46 pm

Re: How to convert a double to a C string?

Tue May 11, 2021 5:23 pm

Yeah, I wouldn't be so sure that snprintf doesn't allocate memory. Have a look at newlib and FreeRTOS: "The most common functions that bite unsuspecting embedded developers are sprintf using %f, dtoa, ftoa, rand, and strtok."

I highly recommend mpaland/printf: Tiny, fast, non-dependent and fully loaded printf implementation for embedded systems.

cleverca22
Posts: 3753
Joined: Sat Aug 18, 2012 2:33 pm

Re: How to convert a double to a C string?

Tue May 11, 2021 6:08 pm

carlk3 wrote:
Tue May 11, 2021 5:23 pm
Yeah, I wouldn't be so sure that snprintf doesn't allocate memory. Have a look at newlib and FreeRTOS: "The most common functions that bite unsuspecting embedded developers are sprintf using %f, dtoa, ftoa, rand, and strtok."

I highly recommend mpaland/printf: Tiny, fast, non-dependent and fully loaded printf implementation for embedded systems.
the pico is already using its own variant of printf, rather then the one your toolchain provides
so it may already be a solved problem, you would have to double-check the disassembly of the final .elf file

zapta
Posts: 52
Joined: Thu May 06, 2021 4:23 pm

Re: How to convert a double to a C string?

Tue May 11, 2021 6:16 pm

Does the Pico's C++ SDK comes out of the box with sprintf that supports int64 and double ? Do I need to enable it somehow?

fivdi
Posts: 440
Joined: Sun Sep 23, 2012 8:09 pm
Contact: Website

Re: How to convert a double to a C string?

Tue May 11, 2021 6:17 pm

carlk3 wrote:
Tue May 11, 2021 5:23 pm
Yeah, I wouldn't be so sure that snprintf doesn't allocate memory. Have a look at newlib and FreeRTOS: "The most common functions that bite unsuspecting embedded developers are sprintf using %f, dtoa, ftoa, rand, and strtok."

I highly recommend mpaland/printf: Tiny, fast, non-dependent and fully loaded printf implementation for embedded systems.

The pico-sdk uses Marco Poland's printf / sprintf / snprintf implementation which doesn't allocate memory on the heap. See https://github.com/raspberrypi/pico-sdk ... f/printf.c

DarkElvenAngel
Posts: 1688
Joined: Tue Mar 20, 2018 9:53 pm

Re: How to convert a double to a C string?

Tue May 11, 2021 7:03 pm

zapta wrote:
Tue May 11, 2021 6:16 pm
Does the Pico's C++ SDK comes out of the box with sprintf that supports int64 and double ? Do I need to enable it somehow?
I haven't tried it but it might work out of the box. I'm not at my desk to try it.

carlk3
Posts: 57
Joined: Wed Feb 17, 2021 8:46 pm

Re: How to convert a double to a C string?

Tue May 11, 2021 8:43 pm

zapta wrote:
Tue May 11, 2021 6:16 pm
Does the Pico's C++ SDK comes out of the box with sprintf that supports int64 and double ? Do I need to enable it somehow?
It works for me.

carlk3
Posts: 57
Joined: Wed Feb 17, 2021 8:46 pm

Re: How to convert a double to a C string?

Tue May 11, 2021 8:47 pm

fivdi wrote:
Tue May 11, 2021 6:17 pm
The pico-sdk uses Marco Poland's printf / sprintf / snprintf implementation which doesn't allocate memory on the heap. See https://github.com/raspberrypi/pico-sdk ... f/printf.c
That's the same implementation that I linked. I forgot that it was included in the SDK.

kilograham
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 618
Joined: Fri Apr 12, 2019 11:00 am
Location: austin tx

Re: How to convert a double to a C string?

Tue May 11, 2021 11:58 pm

yeah floats are on by default; you can turn off via PICO_PRINTF_SUPPORT_FLOAT=0
It is also turned off automatically if you do

Code: Select all

set_pico_float_implentation(my_app none)
in your CMakeLists.txt, as the printf implementation for floats uses the floating point code!

zapta
Posts: 52
Joined: Thu May 06, 2021 4:23 pm

Re: How to convert a double to a C string?

Wed May 12, 2021 5:22 am

Thanks everybody! sprintf() indeed works with doubles and it solved by problem.

However, I am having problems with formatting int64 values. For example this code:

#include <inttypes.h>
...
uint64_t foo = 12345678912346;
printf("%s\n", PRId64);
printf("%d\n", foo);
printf("%" PRId64 "\n", foo);
printf("%ld\n", foo);
printf("%lld\n", foo);
Outputs this:
lld
536883544
ld
268607280
ld

DarkElvenAngel
Posts: 1688
Joined: Tue Mar 20, 2018 9:53 pm

Re: How to convert a double to a C string?

Wed May 12, 2021 1:07 pm

Have a look at this https://stackoverflow.com/questions/922 ... -type-in-c

EDIT::

Code: Select all

    const uint64_t foo = 12345678912346;
    Printf("%lld", foo);
Worked for me I got 12345678912346

My Printf function is a wrapper for vsprintf that write to my VGA screen buffer.

BTW didn't need to include inttypes.h
Last edited by DarkElvenAngel on Wed May 12, 2021 3:26 pm, edited 2 times in total.

kilograham
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 618
Joined: Fri Apr 12, 2019 11:00 am
Location: austin tx

Re: How to convert a double to a C string?

Wed May 12, 2021 2:52 pm

zapta wrote:
Wed May 12, 2021 5:22 am
Thanks everybody! sprintf() indeed works with doubles and it solved by problem.

However, I am having problems with formatting int64 values. For example this code:

#include <inttypes.h>
...
uint64_t foo = 12345678912346;
printf("%s\n", PRId64);
printf("%d\n", foo);
printf("%" PRId64 "\n", foo);
printf("%ld\n", foo);
printf("%lld\n", foo);
Outputs this:
lld
536883544
ld
268607280
ld
Trivial to try, so i did. and it works fine (the lines giving 0 give compiler warnings, are you getting any?)

lld
0
12345678912346
0
12345678912346

Are you using just the SDK or are you using some other environment?

zapta
Posts: 52
Joined: Thu May 06, 2021 4:23 pm

Re: How to convert a double to a C string?

Wed May 12, 2021 3:44 pm

Thanks @kilograham. I am using a platform adaptation of the SDK. Will take the question there.

Does the SDK has a #define or other setting that controls the support of int64 printf support?

Edit: I tried in the past to install the C++ SDK but it was way too complicated, going back to the days of the 'tool-chains'. These days I prefer out of the box installations similar to platformio (a full development environment) or Arduino (a minimalist environment). I hope that RPI will come with a simpler solution, preferably platformio.

fivdi
Posts: 440
Joined: Sun Sep 23, 2012 8:09 pm
Contact: Website

Re: How to convert a double to a C string?

Wed May 12, 2021 4:59 pm

zapta wrote:
Wed May 12, 2021 3:44 pm
Does the SDK has a #define or other setting that controls the support of int64 printf support?

Yes, PICO_PRINTF_SUPPORT_LONG_LONG. See here.

WizIO
Posts: 65
Joined: Mon Feb 22, 2021 8:34 am
Location: Sofia
Contact: Website

Re: How to convert a double to a C string?

Thu May 13, 2021 5:05 am

for PlatformIO

project INI add:
board_build.nano = disable

by default is enabled: -specs=nano.specs

then %llu, %lld, PRIu64 .... is valid

zapta
Posts: 52
Joined: Thu May 06, 2021 4:23 pm

Re: How to convert a double to a C string?

Thu May 13, 2021 4:36 pm

Thanks WizIO, that works.

For anybody that interested, here is compatibility discussion on the platformio side.

https://community.platformio.org/t/my-p ... ce/21407/3

Return to “SDK”