Base mac is shorter for one character

marware
Posts: 4
Joined: Sat Feb 12, 2022 8:15 pm

Base mac is shorter for one character

Postby marware » Sat Feb 12, 2022 8:23 pm

Hi all,

Not sure if happens to anyone else, but one of my devices is reporting a shorter base mac address when converted to a string.
I feel the problem is with conversion, here is the code. Similar code is displayed in example so I'm confused :?

Code: Select all

uint8_t mac[6];
    esp_err_t result;
    result = esp_efuse_mac_get_default(mac);
    if(result != ESP_OK)
    {
        ESP_LOGE(TAG, "Failed to get mac address. Error code: '%d'", result);
        // TODO: restart device
    }
    char deviceId[13];
    sprintf(deviceId,"%x%x%x%x%x%x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
the result is: 943cc6d8878

Have no idea what is happening.

ESP_Sprite
Posts: 9770
Joined: Thu Nov 26, 2015 4:08 am

Re: Base mac is shorter for one character

Postby ESP_Sprite » Sun Feb 13, 2022 7:38 am

Use '%02x' instead of '%x'.

marware
Posts: 4
Joined: Sat Feb 12, 2022 8:15 pm

Re: Base mac is shorter for one character

Postby marware » Sun Feb 13, 2022 2:49 pm

Yes, thanks, I've realized that as soon as I posted :P
The return value of the mac is of course valid and formating was missing 0 before the last character: 943cc6d88708

I would be willing to update example base_mac_address_example_main.c if it make sense to you?

ESP_Sprite
Posts: 9770
Joined: Thu Nov 26, 2015 4:08 am

Re: Base mac is shorter for one character

Postby ESP_Sprite » Mon Feb 14, 2022 1:39 am

As far as I can see it, that example uses "0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x" as the notation, which also makes it clear which digit is which without the need for a leading zero.

Who is online

Users browsing this forum: Google [Bot] and 59 guests