MAC Address to String

Hi everyone,

I'm trying to display-convert MAC address byte array to String, but without any luck, I'm not a big C language expert and I need your help badly! :confused:

I wish to convert this:

static byte cliMacAddr[] = {0x74,0x69,0x69,0x2D,0x30,0x32};

to String to get this:

74:69:69:2D:30:32

I already tried approach like this (result: tii-02):

String myString = (char*)cliMacAddr;

or this one (result: nothing):

void prntmac(){
Serial.print("MAC Address: ");
  for (byte i = 0; i < 6; ++i) {
    Serial.print(cliMacAddr[i], HEX);
    if (i < 5)
      Serial.print(':');
  }
  Serial.println();
  
}

I would like a function to return the MAC Address as String value.

Thanks for your help!

I'm trying to display-convert MAC address byte array to String

Why?

There is almost no reason to have the MAC address as a String.

something like this?

String mac2String(byte ar[])
{
  String s;
  for (byte i = 0; i < 6; ++i)
  {
    char buf[3];
    sprintf(buf, "%2X", cliMacAddr[i]);
    s += buf;
    if (i < 5) s += ':';
  }
  return s;
}

not very efficient but it should work (I think)

Hey PaulS,

I tought that you will be the first responder! :smiley: You are soo funny, keeping this site alive!

PaulS:
Why?

There is almost no reason to have the MAC address as a String.

I wish to display on LCD and use in other data entry/processing.

I wish to display on LCD

You do not need a String for that. The code to do that looks exactly like the code to display the MAC on the serial monitor, except that the destination for the data is different.

and use in other data entry/processing.

Put the tap dancing shoes away. EXACTLY what data entry/processing? The ONLY way that the MAC address is usable is as a collection of bytes. Converting that collection to a String and back to an array of bytes is a waste of resources.

robtillaart:
something like this?

String mac2String(byte ar[])

{
  String s;
  for (byte i = 0; i < 6; ++i)
  {
    char buf[3];
    sprintf(buf, "%2X", cliMacAddr[i]);
    s += buf;
    if (i < 5) s += ':';
  }
  return s;
}




not very efficient but it should work (I think)

Hey robtillaart it's working great, after 1 correction! Thanks! :wink:

String mac2String(byte ar[]){
  String s;
  for (byte i = 0; i < 6; ++i)
  {
    char buf[3];
    sprintf(buf, "%2X", ar[i]);
    s += buf;
    if (i < 5) s += ':';
  }
  return s;
}

PaulS:
You do not need a String for that. The code to do that looks exactly like the code to display the MAC on the serial monitor, except that the destination for the data is different.
Put the tap dancing shoes away. EXACTLY what data entry/processing? The ONLY way that the MAC address is usable is as a collection of bytes. Converting that collection to a String and back to an array of bytes is a waste of resources.

It's because you can't get data in mentioned format

 74:69:69:2D:30:32

Still PaulS has a point you do not need a String version of the byte array to display it on LCD or Serial,
send it over the internet or whatever.

robtillaart:
Still PaulS has a point you do not need a String version of the byte array to display it on LCD or Serial,
send it over the internet or whatever.

Maybe, but I don't know else way to pass byte array to http query....

beic:
Maybe, but I don't know else way to pass byte array to http query....

you could use a char array which uses in general less memory than a String.

But if it works who cares, there is almost always another way to solve the same problem.

The reason PaulS and others (including me) are less font of Strings is that they can fragment the limited memory of the Arduino (esp UNO). We have seen too many sketches that "explode" due to the String class.

just a footnote,

What's your project about?

robtillaart:
you could use a char array which uses in general less memory than a String.

But if it works who cares, there is almost always another way to solve the same problem.

The reason PaulS and others (including me) are less font of Strings is that they can fragment the limited memory of the Arduino (esp UNO). We have seen too many sketches that "explode" due to the String class.

just a footnote,

What's your project about?

My project is about an array of DS18b20 temperature sensors (i2c), 2x Digital kWh meter trough IRQ, and all the info sent by ethernet to the Domoticz server.

Sounds like fun!

robtillaart:
Sounds like fun!

Yeah, it is more struggling than fun, because I'm not so good in Java, Json and C programming, I'm in VB waters about 20 years now...

But you helped me a lot! :wink:

btw...I think that one is the only one String in my whole Sketch the rest are char definitions and I try to use dtostrf and sprintf anywhere if that is a good idea.

if that is the only String you could also solve it this way

byte ar[6] = { 12, 23, 34, 45, 56, 78 };

void setup()
{
  Serial.begin(115200);
  Serial.print("Start ");
  Serial.println(__FILE__);

  char macAddr[18];
  sprintf(macAddr, "%2X:%2X:%2X:%2X:%2X:%2X", ar[0], ar[1], ar[2], ar[3], ar[4], ar[5]);
  Serial.println(macAddr);

  macAddr[0] = '\0';
  for (byte i = 0; i < 6; ++i)
  {
    char buf[3];
    sprintf(buf, "%2X", ar[i]);
    strcat(macAddr, buf);
    if (i < 5) strcat(macAddr, ":");
  }
  macAddr[17] = '\0';
  Serial.println(macAddr);
}

void loop()
{
}

robtillaart:
if that is the only String you could also solve it this way

byte ar[6] = { 12, 23, 34, 45, 56, 78 };

void setup()
{
  Serial.begin(115200);
  Serial.print("Start ");
  Serial.println(FILE);

char macAddr[18];
  sprintf(macAddr, "%2X:%2X:%2X:%2X:%2X:%2X", ar[0], ar[1], ar[2], ar[3], ar[4], ar[5]);
  Serial.println(macAddr);

macAddr[0] = '\0';
  for (byte i = 0; i < 6; ++i)
  {
    char buf[3];
    sprintf(buf, "%2X", ar[i]);
    strcat(macAddr, buf);
    if (i < 5) strcat(macAddr, ":");
  }
  macAddr[17] = '\0';
  Serial.println(macAddr);
}

void loop()
{
}

I tried:

char macAddr[18];
sprintf(macAddr, "%2X:%2X:%2X:%2X:%2X:%2X", ar[0], ar[1], ar[2], ar[3], ar[4], ar[5]);
Serial.println(macAddr);

but it uses a little more of dynamic memory, but it's working fine.

1 Like

but it uses a little more of dynamic memory

It uses no dynamic memory. It uses static memory.

Dynamic memory is allocated using new or malloc() and freed using delete or free().

PaulS:
It uses no dynamic memory. It uses static memory.

Dynamic memory is allocated using new or malloc() and freed using delete or free().

When I used that mentioned line the dynamic memory usage increased by 1%

It's a simple ask. I get the impression that people would rather quibble ("What? Why would you want to do that?") than code.

sprintf is pretty heavyweight. Converting bytes to hex digits is not challenging.

static byte cliMacAddr[] = {0x74,0x69,0x69,0x2D,0x30,0x32};

void foo() {
  char macBuf[18]; // 12 digits, 5 colons, and a '\0' at the end
  macAddrToString(cliMacAddr, macBuf);
  Serial.println(macBuf);
}


void macAddrToString(byte *mac, char *str) {
  for(int i = 0; i<6; i++) {
    byte digit;
    digit = (*mac >> 8) & 0xF;
    *str++ = (digit < 10 ? '0' : 'A'-10) + digit;
    digit = (*mac) & 0xF;
    *str++ = (digit < 10 ? '0' : 'A'-10) + digit;
    *str++ = ':';
    mac ++;
  }
  // replace the final colon with a nul terminator
  str[-1] = '\0';
}

String(WiFi.macAddress())

worked great for me.

Got output that looked like this:

5C:CF:7F:23:CD:3B

Perfect!