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() {}
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?
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
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.
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 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.
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.