Add String object to character array in Arduino

I am using Arduino. I would like to add a String object to an array of characters.

String msg = "ddeeff"

char charArr[1600];

//assume charArr already contains some string
//How can I do something like this to append String to charArray?
charArr = charArr + msg;
+4
source share
2 answers

This will work for an Arduino String object.

strcat( charArr, msg.c_str() );

The String object is msgconverted to an array of characters using the String c_str () method. Then you can use strcat () to add 2 character arrays.

As Rakete1111 mentioned, undefined behavior if charArrnot large enough

+2
source

Stringhas operator+, which takes const char*, and also has a function c_str()that converts it to const char*.

:

String temp = charrArr + msg; //Store result in a String

//Copy every character
std::strncpy(charArr, temp.c_str(), sizeof(charrArr));
+1

Source: https://habr.com/ru/post/1651625/


All Articles