MAC address store in SD card : Convert char* to byte

Hey guys,

I've stored the MAC address of my ethernet shield on an SD.
I wish to read the address from the SD card and fill my byte array.

But I don't know how to convert a char* to a byte.

Config file :

shutter_osc_Mac : 0xDE:0xAD:0xBE:0xEF:0xFE:0xED

Code :

byte tempMac[6];
      char* t;
      Serial.print("T0 : ");
      Serial.println(value);
      tempMac[0] = (strtok_r(value, ":", &t));
      tempMac[1] = (strtok_r(t, ":", &t));
      tempMac[2] = (strtok_r(t, ":", &t));
      tempMac[3] = (strtok_r(t, ":", &t));
      tempMac[4] = (strtok_r(t, ":", &t));
      tempMac[5] = (strtok_r(t, ":", &t));

But I don't know how to convert a char* to a byte.

You can't (sensibly).

But you can convert what the pointer points to (a char) to a byte simply by telling the compiler a simple lie.

byte x = (byte)*charPtr;

It doesn't work, I should have OxDE and I got 30.

      Serial.println((byte)*strtok_r(value, ":", &t), HEX);

Get rid of strtok_r(). That is the re-entrant version that only makes sense on a multi-threaded system. Get rid of the pointer. It is NOT needed with strtok(), which is what you should be using.

1 Like

Use strtol to convert; you can specify the base

  // simulate retrieval of MAC
  char *MAC;
  MAC = (char*)calloc(30, 1);
  strncpy(MAC, "0xDE:0xAD:0xBE:0xEF:0xFE:0xED", 30);

  // work variables
  char *token;
  char *ptr;

  // get first token
  token = strtok(MAC, ":");
  while(token != NULL)
  {
    // convert
    byte b = strtol(token, &ptr, 16);
    // print
    Serial.println(b, HEX);
    // next token
    token = strtok(NULL, ":");
  }

  Serial.println(MAC);

The last println is only added to demonstrate that strtok is destructive; see strtok(3) - Linux man page under bugs (although they ain't bugs).

Reference for strtol: strtol(3) - Linux man page

Thanks to you two !
It works ! But only for the first byte --"

char *ptr, *t;
      byte tempMac[6];

      Serial.print("T0 : ");
      Serial.println(value);

      t = strtok(value, ":");
      Serial.println(t);
      tempMac[0] = strtol(t, &ptr, 16);

      t = strtok(NULL, ":");
      Serial.println(t);
      tempMac[1] = strtol(t, &ptr, 16);

      t = strtok(NULL, ":");
      Serial.println(t);
      tempMac[2] = strtol(t, &ptr, 16);

      t = strtok(NULL, ":");
      Serial.println(t);
      tempMac[3] = strtol(t, &ptr, 16);

      t = strtok(NULL, ":");
      Serial.println(t);
      tempMac[4] = strtol(t, &ptr, 16);

      t = strtok(NULL, ":");
      Serial.println(t);
      tempMac[5] = strtol(t, &ptr, 16);

I've this on the Serial :
T0 : 0xDE:0xAD:0xBE:0xEF:0xFE:0xED
0xDE
0xAD
0xBE
0xEF
0xFE
0xED
Shutter Mac : DE:7F:0:20:D4:3

Maybe the fine folks at http://snippets-r-us.com can help you with your snippets.

For debugging, it might be useful to add an additional println to show the content of the relevant tempMac. So you can verify that it contains the correct value.

Further your code does not contain code that prints the last line 'Shutter Mac : DE:7F:0:20:D4:3'. I suspect that that is what fails. When I print the tempMac array to the serial port, I see the correct values.

It might be advisable to cast the result of the strtol() function to a byte before assigning it to the byte; not sure if it's the cause of the issue (I did not do it in my code running on an Uno).