Reputation:
I am using Arduino. I would like to append 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;
Upvotes: 5
Views: 10386
Reputation: 28030
This will work for Arduino String object.
strcat( charArr, msg.c_str() );
String object msg
gets converted into an array of characters with the String method c_str(). Then, you can use strcat() to append the 2 arrays of characters.
As mentioned by Rakete1111, it is undefined behavior if charArr
is not big enough
Upvotes: 4
Reputation: 48938
String
has a operator+
which takes a const char*
, and it also has a c_str()
function, which converts it to a const char*
.
You can combine them to get the desired result:
String temp = charrArr + msg; //Store result in a String
//Copy every character
std::strncpy(charArr, temp.c_str(), sizeof(charrArr));
Upvotes: 1