byte* to individual values | PubSubClient

Hi all,
I need help with my MQTT Payload which contains 10 digits.
I am using the PubSubClient library.
What I want to do is recieve a 10 digit number e.g. "5000302510" then I want to seperate them every 2 digits. Something like this -> "50" "00" "30" "25" "10" and store them in seperate int variables.
If it would help I can also modify the Payload from the Sender side. But I want it to be only one Message which contains the information.
At the Moment I have this code which does not work. The first conversion works so var rec1 holds the right value but everything after that is not correct.

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  String value = "";
  byte* payloadMan = payload;
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]); 
  }
  Serial.println();
  payloadMan[0] = payload[0];
  payloadMan[1] = payload[1];
  payloadMan[2] = '\0';
  rec1 = atoi((char *)payloadMan);
  payloadMan[0] = payload[2];
  payloadMan[1] = payload[3];
  payloadMan[2] = '\0';
  rec2 = atoi((char *)payloadMan);

While arrays and pointers are often interchangeable, they are not always.

Your payloadMan pointer points to the same memory that payload uses. So, when you assign a value to payloadMan, you are assigning a value to payload, too. By the time you start trying to extract the data from rec2, you've already crapped on the data that is supposed to be rec2.

Create another array, NOT a pointer. Copy the data from one array to the other.

Thank you very much, this worked just how I wanted it. :slight_smile: