3

I have an Arduino Duemilanove.

I read this page on string concatenation. Yet, it didn't include my case.

I have a non constant character array, that later gets a value.

char clat[11];
dtostrf(gps.location.lat(), 11, 6, clat);

I want to output this:

Latitude: 20.653425

So basically I want a final string that contains: "Latitude:" + the value of the nonconstant array.

Is it possible?

dda
  • 1,595
  • 1
  • 12
  • 17
user1584421
  • 1,425
  • 3
  • 26
  • 36

2 Answers2

6
char clat[10+11+1] = "Latitude: ";
dtostrf(gps.location.lat(), 11, 6, clat+10);

Since you've named the buffer 'clat' I'm assuming it will always be used for latitude, so we've pre-initialized its size and contents, and we know where to begin the latitude value.

By the way, don't forget to size string buffers one more byte than the length of the maximum length string you intend to put in it.

Update 6/23/14:

From your other question and your comment to this answer, I'll revise my suggestion to this. The ..printf() functions are particularly easy to use, saves you (mis)counting buffer positions, and don't add much lot more code than the Serial() object already includes:

char outbuf[40+1];      // wider than nec, easier than counting
char clat[15+1]; clong[15+1]; // workspaces

sprintf(outbuf, "Latitude/Longitude: %s %s",
    dtostrf(gps.location.lat(), 10, 6, temp),
    dtostrf(gps.location.lng(), 10, 6, temp) );

This works because dtostrf() returns the address of the buffer you gave it; sprintf then takes that string (two of them in this case) when building your final output string.

The printf() functions are often deprecated for embedded systems. They're large (but see my comment about that above); and they're non-reentrant, so don't do that! In these low-end systems we don't often multi-thread anyway - just be aware of that if you do. And even if you have to remove them later and go back to printing piece by piece, they can save you a lot of headaches during development.

Here's another way that avoids both printf and the 32 bytes of work buffers:

char outbuf[40+1];

 strcpy(outbuf, "Latitude/Longitude: ");
 dtostrf(gps.location.lat(), 12, 6, outbuf+strlen(outbuf));
 dtostrf(gps.location.lng(), 12, 6, outbuf+strlen(outbuf));

We can't initialize outbuf unless it is 1) static, and 2) not going to be used more than once, since its initial conditions won't be the same the second time around. I've increased the field-widths of dostrf() to be sure the outputs of the won't be run-together for large values.

JRobert
  • 15,407
  • 3
  • 24
  • 51
3

It's possible and very easy to do. You can convert a character array to a string object with the String(char_array_name_here) function.

To do this you can just do this:

String output = "Latitude: " + String(clat);  //Make sure to add a space so it's not "Latitude:20.653425"

You can do this with any string, just remove the String() part because you don't need to convert a string to a string!

One thing to note: you can just do this code if you're outputting it to the serial console:

Serial.println("Latitude: " + clat);

Summary of edits: found a function I was searching for yesterday but couldn't find. Much simpler


Other links:

Anonymous Penguin
  • 6,365
  • 10
  • 34
  • 62