sprintf for float for Mega2560

hello,

as the Mega2560 provides enough RAM, I need to have sprintf for float also for this board:

e.g.,
sprintf(cbuf, "&f0=%f;&f1=%f;&f2=%f;&f3=%f;&f4=%f;&f5=%f;", f0,f1,f2,f3,f4,f5 );
Serial.println(cbuf);

how can I get this feature also for the Mega2560?

use dtostrf()

double f1 = 1.23, f2 = -4.56789;
char cbuf[100];

void setup() {
  char tmpBuf[20]; // it's your responsibility to have a large enough buffer for the largest number
  Serial.begin(115200);
  cbuf[0] = '\0';
  strcat (cbuf, dtostrf(f1, 0, 3, tmpBuf)); //3 is the number of decimal digits you want
  strcat (cbuf, " and ");
  strcat (cbuf, dtostrf(f2, 0, 3, tmpBuf)); // here our number will be rounded at 3 decimal
  Serial.println(cbuf);
}

void loop() {}

You'll see in the Serial monitor

[tt][color=purple]1.230 and -4.568
[/color]

[/tt]

thanks, but dtostrf is too cumbersome when I have to handle multiple floats in 1 single format string.
That's why I want to have sprintf.

(In my example above it was just about 6 float variants,
sprintf(cbuf, "&f0=%f;&f1=%f;&f2=%f;&f3=%f;&f4=%f;&f5=%f;", f0,f1,f2,f3,f4,f5 );
but actually sometimes it's about dozens)

How can I link to that lib which is already available for all ARM boards and ESPs?
Or how can I patch the libc for the Mega manually, arbitrarily?

may be check this (have not tried)

Usually there is no need to build long buffers.. what do you plan to do with this big buffer (looks like parameters for a URL)? why can't you print things one after another when you send your HTTP request?

there actually is a need for long buffers, it's for UART communication :wink:
But tbh, I would not like to change platform.txt etc, I just want to have the full sprintf / vsprintf functionality out of the box, like for ARM platforms.

there actually is a need for long buffers, it's for UART communication

UART transmission is asynchronous, one character at a time. No requirement for buffers.

The instructions for enabling floats in sprintf() etc. are posted on the forum. Google will find them

there actually is a need for long buffers, it's for UART communication

That's just about the worst reason.

jremington:
UART transmission is asynchronous, one character at a time. No need for buffers at all.

perhaps, yes, but it's too cumbersome to build all single tokens one by one, that's why I want sprintf , to build the entire msg string in 1 single step for all and everything.

Anyway, my question is not about my motivation, my question is about how to ´get sprintf for floats also for the Mega.

I'm pretty sure the required patches are well-documented, but you need to put in a little effort with Mr Google.

Be careful what you wish for.

AWOL:
I'm pretty sure the required patches are well-documented, but you need to put in a little effort with Mr Google.

Be careful what you wish for.

the hint about Google is not very helpful, what I wanted is a more detailed reply, tbh.... :stuck_out_tongue:

If you try, Google will find the detailed instructions on how to enable floats in sprintf().

is this the common attitude here in this forum if someone asks for help, to reply then "ask Google!"
?

dsyleixa:
is this the common attitude here in this forum if someone asks for help, to reply then "ask Google!"
?

If that's the best place to get help, then, yes.

It's what the forum search does.

I have to disagree: if you know the answer, then tell it, if not, just ignore the question and answer nothing.
"Ask Google" is snooty and upstage and won't help anyone.

is this the common attitude here in this forum if someone asks for help, to reply then "ask Google!"

Pretty much, if that is how the hard-to-remember details can be found.

Since I have successfully done that search in order to learn how to enable floats in sprintf, I don't see a compelling reason to do it for you.

I will give you a hint, though: the required details are posted somewhere in this very forum!

I have to disagree: if you know the answer

I think we’ve had this disagreement before.

I don’t know the answer, but I do know answers are out there.

I think the analogy I used last time was map-based.

I don’t know how to get to, for instance, Asnieres-sur-Seine.
But I know that Asnieres-sur-Seine exists, and that it is therefore on a map.
The very best place I know to buy paper maps is Stanfords in Covent Garden.
I know how to get to Stanfords, so that’s where I’ll send you.

(I lied about Asnieres - I used to live there)

jremington:
Pretty much, if that is how the hard-to-remember details can be found.

Since I have successfully done that search in order to learn how to enable floats in sprintf, I don't see a compelling reason to do it for you.

I will give you a hint, though: the required details are posted somewhere in this very forum!

not as far as I can see, and if I had found it, I wouldn't have asked!

dsyleixa:
not as far as I can see, and if I had found it, I wouldn't have asked!

It's hard to tell sometimes - some people are lazy, some need spoonfeeding, some people have poor search skills.

IMO a moderator should moderate, not insult and escalate.

You are entitled to your opinion, and to its expression.

I hope you don't think moderators are somehow inferior in their rights.