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

2
  • 1
    is this a school assignment?
    – jsotola
    Commented Feb 11, 2019 at 21:26
  • Do you know an upper limit on how many there will be?
    – Majenko
    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(){}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.