0

How i can convert a MAC string, like "5D:41:8F:32:34:H2" to byte array like {0x5D, 0x41, 0x8F, 0x32, 0x34, 0xH2}

I'm using an Arduino WOL library and it requires the MAC to pass it as an array of bytes, but I keep them as strings separated by ":" and I have no idea how that conversion could be done.

I can not put more details if you try because I do not even know where to start trying.

void arrancarPC(String strMac) {
  byte mac[] = {0x5D, 0x41, 0x8F, 0x32, 0x34, 0xH2};
  WakeOnLan::sendWOL(broadcast_ip, UDP, mac, sizeof mac);
}
Bosz
  • 359
  • 6
  • 15

1 Answers1

1

the bytes in array must be in reverse order then printed. H2 is not a valid hex value.

void str2mac(const char* str, uint8_t* mac) {
  char buffer[20];
  strcpy(buffer, str);
  const char* delims = ":";
  char* tok = strtok(buffer, delims);
  for (int i = 5; i >= 0; i--) {
    mac[i] = strtol(tok, NULL, 16);
    tok = strtok(NULL, delims);
  }
}

void setup() {
  Serial.begin(115200);

  const char* str = "5D:41:8F:32:34:F2";
  uint8_t mac[6];

  Serial.println(str);

  str2mac(str, mac);

  for (int i = 5; i >= 0; i--) {
    if (i < 5) {
      Serial.print(':');
    }
    Serial.print(mac[i], HEX);
  }
  Serial.println();

}

void loop() {

}
Bosz
  • 359
  • 6
  • 15
Juraj
  • 3,490
  • 4
  • 18
  • 25