Concatenate strings in Arduin

Asked

Viewed 367 times

0

How can I concatenate two or more strings using the language C with Arduino.

I’m following several examples I’ve found around, but it always makes a mistake. "Conversion from 'unsigned char' to 'String' is ambiguous"

I’m using the .concat()

void VuetifyDateTime(int hours,int minutes)
{
      String hourSt = String(hours);
      String minutesSt = String(minutes);
      
      String dateTime = hourSt.concat(minutesSt);
      
      TM1637(dateTime.toInt());
  • This String type is from a library or is a typedef of yours?

  • It’s from Arduino itself. String class

  • hourSt.concat(String(minutes)); should remove ambiguity. According to the reference, this function accepts String type parameters. https://www.arduino.cc/reference/pt/language/variables/data-types/string/functions/concat/

  • It worked. Want to post as response?

  • No need, it was just a type casting problem and this is trivial. I believe the type String have some operator who does the automatic conversion to unsigned char and as Concat accepts many different types, the compiler gets confused by the ambiguity. std::string is more reliable.

1 answer

0

Can be done this way, remember to do the result pointer free

    const size_t len1 = strlen(s1);
    const size_t len2 = strlen(s2);
    char *result = malloc(len1 + len2 + 1); // +1 para o \0
    memcpy(result, s1, len1);
    memcpy(result + len1, s2, len2 + 1); // +1 para o \0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.