concatenate a char* with float

i have latitude and longitude stored in two float variables.
I need a string like this: "lat=23.03&lon=72.56" in form of char*. sprintf and dtostrf are not working for me. I have to write a function which returns char* when called. Please help.

Not particularly informative there sport..... sprintf and dtostrf not working? Why? What did YOU do wrong? :wink: :stuck_out_tongue:

Some example code that YOU made to show YOUR mistakes would be really helpful to help YOU figure out where YOU went wrong...

Happy new year,

Regards,

Graham

EDIT:

float lat = 23.03;
float lon = 72.56;
char buf[60];
void setup() {
  Serial.begin(115200);
  sprintf(buf, "lat=%5.2f&lon=%5.2f\n", lat, lon);
  Serial.println(buf);
}
void loop() {
}

Produces :-

lat=23.03&lon=72.56

So like I said, what did YOU do wrong? :stuck_out_tongue_closed_eyes:

I am surprised at the output you got considering that by default the Arduino installation does not support formatting of floats.

A much more likely output is

lat=    ?&lon=    ?

Would you like me to just get out my MEGA and test on that also??? Sorry if I only tested on my DUE!!! You got anything else to say about my posts????

Graham

EDIT: So Helibob is right..... That only works on the DUE apparently..... But you could try to be a little more constructive!!

But you could try to be a little more constructive!!

The only thing that I would do differently in the same circumstances would be to explain that I had tested your code on a Uno and got the result I posted.

UKHeliBob:
The only thing that I would do differently in the same circumstances would be to explain that I had tested your code on a Uno and got the result I posted.

And that would have been beneficial to both myself AND the OP.... Thank you... I dumped my MEGA right after getting my DUE, since is just vastly better in all respects, so quirks and foibles of that nature mean nothing to me....

Taking into account recent knowledge learned from UKHeliBob, this version will work on ALL Arduinos....

float lat = 23.03;
float lon = 72.56;
char buf[60];
char buflat[7];
char buflon[7];
void setup() {
  Serial.begin(115200);
  sprintf(buf, "lat=%s&lon=%s\n", dtostrf(lat, 5, 2, buflat), dtostrf(lon, 5, 2, buflon));
  Serial.println(buf);
}
void loop() {
}

Happy New Year,

Graham

Often sprintf() is an H-bomb to kill an ant and, as a result, I'm not a big fan. The following mod works exactly the same, but reduced the code size from 4994 to 3572, saving 1422 bytes on an Uno running 1.5.8.

float lat = 23.03;
float lon = 72.56;
char buf[60];
char buflat[7];
char buflon[7];
void setup() {
  Serial.begin(115200);
  strcpy(buf, "lat=");
  strcat(buf, dtostrf(lat, 5, 2, buflat));
  strcat(buf, "lon=");
  strcat(buf, dtostrf(lon, 5, 2, buflon));
  Serial.println(buf);
}
void loop() {
}

I like your version too!

I am still learning when it comes to efficiency of memory both Flash and SRAM when it comes to different ways of achieving the same end result.

Thankyou for that, it all helps to have other ways shown to us!

Happy new year.

Graham

ghlawrence2000:
Not particularly informative there sport..... sprintf and dtostrf not working? Why? What did YOU do wrong? <...>
So like I said, what did YOU do wrong? :stuck_out_tongue_closed_eyes:

This certainly is not a nice way to begin with a newbie. If you did not get enough sleep last night, take a nap. Once the Op has a good supply of 2-digit posts, then you can be a little terse. I do not think you response is cute and no emoticon is going to take the edge off that response.

Ray

Maybe so, but almost zero information provided? Not a good start point is it?

If you want to talk about terse.... I have received worse than than the reply I gave... And I did sort of redeem myself by at least trying to help the guy, which again is also more than I have previously received.

In fairness MrBurnette, your replies are generally helpful and pleasant, but you are by no means typical. There are a few 'friendlies' on here, but they are certainly not the norm, so forgive me if I bit a little harder than I should.

Regards,

Graham

ghlawrence2000:
In fairness MrBurnette, your replies are generally helpful and pleasant, but you are by no means typical. There are a few 'friendlies' on here, but they are certainly not the norm, so forgive me if I bit a little harder than I should.

Regards,

Graham

Graham,

It's not about forgiveness ... you did not harm to me; I was not insulted by your response. It is about a culture of helpfulness, I try but do not always achieve it either, we are but human and I surely have had a few 'bad' days before.

However, if we do not try and be nice to newbies, we may lose great talent in the future. I'm old and someone will need to replace me in the future. I'd rather grow that talent than hope that fate will provide for it. Once a newbie has 20'ish or so posts, there is an expectation that they should begin to 'help us, help them' by providing valuable information in their initial inquiry.

Were you not impressed that VikasRoy even knew he needed a return pointer of type char?

Thanks for the dialog.

Ray

Cool!! I just clicked your link!! That will keep me happy for a while :smiley:

I will think more carefully in future about my response.

Sorry to VikasRoy.

Happy new year everyone.

Graham

mrburnette:
Were you not impressed that VikasRoy even knew he needed a return pointer of type char?

Yes :wink: actually. I had major problems with pointers, to be fair, still no expert.

Thank you guys. I was using Diecimila. solution given econjack worked for me. Thank you again :slight_smile: