Newbie: In the first line I have my string put together. I need to convert the string to char, so it can be used in the second line. Thanks in advance!!!
String mydata = t + "," + h + "," + c +"," + "1";
char *hello = (mydata);
Newbie: In the first line I have my string put together. I need to convert the string to char, so it can be used in the second line. Thanks in advance!!!
String mydata = t + "," + h + "," + c +"," + "1";
char *hello = (mydata);
We don't recommend using Strings.
char *hello = "some character string";
jremington:
We don't recommend using Strings.
Certainly tools of the devil (or newbies). But just in case, how about:
char hello[20];
[...]
String mydata = t + "," + h + "," + c +"," + "1";
[...]
mydata.toCharArray(hello, 20);
And that's untested code resulting from a google search using the terms "arduino convert string to char array" and taking the first result returned. I guess it's correct, I don't use String.
Regards,
Brad.
Alternatively:-
String mydata = t + "," + h + "," + c +"," + "1";
char *hello = (char*)mydata.c_str();
But as mentioned, it's far better to use char arrays right from scratch, rather than the String class.
To be more constructive, assuming t, h, c etc. have been declared as standard character arrays, you can use sprintf to string them together:
char hello[20]={0};
sprintf(hello,"%s,%s,%s,%s",t,h,c,"1"); //similar to String mydata = t + "," + h + "," + c +"," + "1";
or concatenate successive character arrays
char hello[20]={0};
strcpy(hello,t); //hello = copy of t
strcat(hello,","); // append a comma
...etc.
Take a look at the suggestions the others have made in the following code:
void setup() {
Serial.begin(9600);
/*
// Flavor #1 -- 3594 bytes
String t = "First";
String h = "Second";
String c = "Third";
String mydata = t + "," + h + "," + c +"," + "1";
char *hello = (char*)mydata.c_str();
*/
// Flavor #2 -- 3372 bytes
/*
char t[] = "First";
char h[] = "Second";
char c[] = "Third";
char hello[20]={0};
sprintf(hello,"%s,%s,%s,%s",t,h,c,"1");
*/
// Flavor #3 -- 1978 bytes
char t[] = "First";
char h[] = "Second";
char c[] = "Third";
char hello[20]={0};
strcpy(hello, t);
strcat(hello, ",");
strcat(hello, h);
strcat(hello, ",");
strcat(hello, c);
strcat(hello, ",");
strcat(hello, "1");
Serial.print(hello);
}
void loop() {
}
The first uses the String class while the second uses char arrays, but the sprintf() function to format the data. These first two are very powerful since both bring a lot to the table. However, in this application, you are only using a very small fraction of that power. The String class has lots of convenient features (crutches?) and beginners like it because it's easy to use. However, why add the extra weight of training wheels? The same is true for sprintf()--it's very powerful and does away with the String class and all its bloat, but sprintf() is also very powerful...more power than you need here, hence also bloats the code bigger than need be. The last version is simply char arrays and the str*() family of string processing functions. Simple tasks should have simple solutions, which is why the code size is pretty small by comparison.
These are some of the reason why most people here don't use the String class that often.
@econjack: Very nice and useful comparison! Thanks!
I was thinking of pointing out (but forgot) that sprintf() also formats variables into strings, so you get all that extra power for "free" once the basic module is loaded. e.g.
sprintf(hello,"%s,%s,%s,%lu",t,h,c,millis()); //appends the current value of millis() to the string
To be more constructive, assuming t, h, c etc. have been declared as standard character arrays
I doubt that that is a reasonable assumption. I suspect that t and h at least are temperature and humidity and are floats.