1

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

asked Feb 11, 2019 at 20:58
2
  • 1
    is this a school assignment? Commented Feb 11, 2019 at 21:26
  • Do you know an upper limit on how many there will be? Commented Feb 11, 2019 at 21:54

1 Answer 1

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(){}
answered Feb 11, 2019 at 22:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.