Concatenating from Arrays

Hello again!

So in my code, buf[0] = "h" and buf[1] = "i". But when I try to concatenate by doing Serial.print(buf[0] + buf[1]), it adds up their DEC values instead.. How can I go about concatenating so that I can serial print "hi"?

Thanks!

The quick and dirty way is

Serial.print(buf[0]); Serial.print(buf[1]);

If there is nothing in the buf[2] location and the array is at least 3 bytes long

buf[2] = '\0'; Serial.print(buf);

Will there always be two chars?

Or will you always know the number of chars?


Rob

Wow, thanks Rob for your quick response… I actually just figured it out. So for anyone who might have a similar problem, here’s how:

    String note;
    for (i = 0; i < buflen; i++)
      {
      //Serial.print(buf[i]);
      note += buf[i];
      
      }
    Serial.print(note);

Yep that will work, String is a very inefficient class to use though.

My second example will do essentially the same in a single line.


Rob