Hello,
How do I take all the values from my int array_unit and make it into one char?
The char needs to include the commas "," so it would read "0,1,2,3,4,5,6,7,8,9"
int array_unit[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Thank you
Hello,
How do I take all the values from my int array_unit and make it into one char?
The char needs to include the commas "," so it would read "0,1,2,3,4,5,6,7,8,9"
int array_unit[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Thank you
A 'char' only holds one character, not 10.
Would it make more sense to use a string then?
something like this?
#define ARR_SIZE 10
void setup() {
// put your setup code here, to run once:
int array_unit[ARR_SIZE] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
char c_str[50] = "";
char temp[10] = "";
Serial.begin(115200);
//----------------------------------------------//
for (int i = 0; i < ARR_SIZE; ++i) {
sprintf(temp, "%d,", array_unit[i]);
strcat(c_str, temp);
}
c_str[strlen(c_str) - 1] = '\0';
//----------------------------------------------//
//or simply....
//sprintf(c_str, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", array_unit[0], array_unit[1], array_unit[2], array_unit[3], array_unit[4], array_unit[5], array_unit[6], array_unit[7], array_unit[8], array_unit[9]);
//----------------------------------------------//
Serial.println(c_str);
}
void loop() {
// put your main code here, to run repeatedly:
}
0,1,2,3,4,5,6,7,8,9
hope that helps....
sherzaad,
AWESEOME!!!
I like the "for" method you provided since I will be using a longer array on my project.
You also introduced me to "sprintf" which is new for me.
Appreciate it.