Convert and print MAC address as six character string

Edit: My problem seemed to be that the Local/Global Bit is honored on this platform. Good on them.

I am trying to print out a mac address as a six character ascii string, rather than 6 hexidecimal values. I set the MAC address to an ascii char array successfully.

void OnDataRecv(uint8_t * mac,uint8_t *payload, uint8_t len){ // espnow receive
Serial.print((char) mac[0]);
Serial.print((char) mac[1]);
Serial.print((char) mac[2]);
Serial.print((char) mac[3]);
...etc...

}

This seems to print chars of the pointer, rather than the contents of the addresses.

consider

deadbeef1234


byte mac [] = { 0xde, 0xad, 0xbe, 0xef, 0x12, 0x34 };
char s [80];

// -----------------------------------------------------------------------------
void setup () {
    Serial.begin (9600);

    sprintf (s, "%02x%02x%02x%02x%02x%02x",
        mac [0], mac [1], mac [2], mac [3], mac [4], mac [5]);
    Serial.println (s);
}

void loop ()
{
}

It should be doing what you wanted. The pointer is only two bytes long so it can't be printing the six bytes of a two-byte value.

A more direct version is:
Serial.write(mac, 6); // Send the six characters of the MAC

1 Like

That's rather dangerous, since the low two bits of the first byte have special meanings! (the low bit indicates whether the address is unicast or multicast.)
(why "the low bit"? Because it's transmitted first.)

Thanks. Maybe I will use 5 bytes.

Looks like the problem is when we are setting the MAC address. Thanks!

Be warned that the MAC is supposed to be globally unique and assigned by the hardware manufacturer.

1 Like

It certainly is, but Apple and other manufacturers no longer honor it. Random MACs from iphones will have this bit set randomly. Good to see this one doesn't do that.

The 2nd lowest bit means "locally administered." It's set by protocols (eg DECNet) that want their MAC address and protocol address to be intimately related (not required for IP!)
(this means you WANT to set it, if you're picking the rest of the bytes "randomly.")
The subset of ascii that ends in 0b10 is "&*.26:>BFJNRVZ^bfjnrvz~
">BillW" sounds like a fine MAC address!

It appears that the only bit that causes a problem is the low order (unicast/multicast)bit. In fact now we are able to use the 7bits + 5bytes of MAC for payload. These devices are communicating using a very minimal ESPNow so the MAC address doesn't really matter for routing etc.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.