Append String to array of characters

I would like to append a String object to an array of characters.

My code looks something like this;

String msg = "123456"

char charArr[1600];

//assume charArr already contains some string
//How can I do something like this to append String to charArray?
charArr = charArr + msg;
int sizeOf = 6;
int i = 0;
int offset = 0;

char charArr[1600];

String msg = "123456";

while((i<sizeOf) && (charArr[i+offset] != '\0') && (msg[i] != '\0'))
{
   charArr[i+offset]=msg[i];

   i++;
}

This should technically work since strings are just arrays of chars. If it doesn't, than use msg as an array of chars explicitly. Do you need msg to be of type string specifically?

Give it a quick try first.

You will need to keep track of where on char array the current position is.

Another way of doing it:

char charArr[1600];
int actualcharposistion = 500;

String msg = "123456";

for(int a=0;a<msg.length();a++){
actualcharposistion++;
charArr[actualcharposistion] = msg.charAt(a);
}

the real question is why the h*ll do you want to doString msg = "123456";versus char msg[] = "123456"; if you have no good reason to use the String class, just don't...

if you really need to have a String (with a capital S) then the class has a method msg.c_str() that will convert the contents of msg to a C-style, null-terminated char array. so you could just use strcat to concatenate the charArr and the msg.c_str()

(side note 1600 bytes depending on your arduino is a lot of memory)

DanDare:
You will need to keep track of where on char array the current position is.

Another way of doing it:

char charArr[1600];

int actualcharposistion = 500;

String msg = "123456";

for(int a=0;a<msg.length();a++){
actualcharposistion++;
charArr[actualcharposistion] = msg.charAt(a);
}

I did just that using the "offset" variable added to the variable "i".

That way you can choose where to write the data in charArr by changing "offset".

Power_Broker:
I did just that using the "offset" variable added to the variable "i".

That way you can choose where to write the data in charArr by changing "offset".

that will leave you possibly with a charArr not terminated by a '\0' is you write beyond the previous end of charArr

I found a one-liner answer to my question.

strcat( charArr, msg.c_str() );

lightaiyee:
I found a one-liner answer to my question.

strcat( charArr, msg.c_str() );

You made this discovery by looking at post #3 in this thread ?

6v6gt:
You made this discovery by looking at post #3 in this thread ?

Oh well hopefully the OP did read the suggestion...