I am sending a string to the Arduino via serial, I made the program that sends the data, so I can change it to anything, but for now it sends a string of hex numbers delimited by ':', so for example 00:AA:FF and I need to get that into an int array onto the Arduino, the main problem is I don't always know how big the array will be, sometimes just 2 indexes, other times 16 indexes.
As I said, I made the program sending the data so I can make the data anything, I just need to get an int array to the Arduino and get it back as an int array via serial
-
1is this a school assignment?jsotola– jsotola2019年02月11日 21:26:58 +00:00Commented Feb 11, 2019 at 21:26
-
Do you know an upper limit on how many there will be?Majenko– Majenko2019年02月11日 21:54:22 +00:00Commented Feb 11, 2019 at 21:54
1 Answer 1
You could store the hex numbers in a int array
using strtok() and strtol().
char str[] = "00:AA:FF:7FFF";
int intArray[16]; // Maximum number of hex numbers.
void setup(){
Serial.begin(9600);
int counter = 0;
char * pch = strtok(str, ":");
while(pch != NULL){
intArray[counter] = strtol(pch, NULL, 16);
pch = strtok(NULL, ":");
counter++;
}
Serial.print("Item Count = ");
Serial.println(counter);
for(int i = 0; i < counter; i++){
Serial.println(intArray[i]);
}
}
void loop(){}