Parsing MAC address

I have the code that should get MAC and IP addres from POST request:

    char name[2][50];
    char value[2][50];
    server.readPOSTparam(name[0], 50, value[0], 50);
    server.readPOSTparam(name[1], 50, value[1], 50);
    Serial.println(name[0]); //there is "mac" string
    Serial.println(value[0]); //mac address
    Serial.println(name[1]); //"ip" string
    Serial.println(value[1]); //ip address

    uint8_t ipAddr[4];
    uint8_t macAddr[6];
    sscanf(value[0], "%x:%x:%x:%x:%x:%x", &macAddr[0], &macAddr[1], &macAddr[2], &macAddr[3], &macAddr[4], &macAddr[5]);
    sscanf(value[1], "%u.%u.%u.%u", &ipAddr[0], &ipAddr[1], &ipAddr[2], &ipAddr[3]);
   Serial.println(macAddr[0], HEX);
   Serial.println(macAddr[0], DEC);

When I'm parsing MAC address in sscanf in macAddr[0] thre is 0 value. Next parts of macAddr are ok...


What's wrong?

I had a quick look an I noticed two things:

  1. server.readPOSTparam() - from which library is this
  2. the value 50 for the length of the char arrays seems a bit large (20 should be enough) Could you post the whole code?

What platform are you using?

1) It's from webduino ( 2) I changed to 25, full code

I think that MAC is readed ok from POST data - in second line of output there is full MAC address...

I'm using 0021 on Windows...

scaning the whole code , the first thing I would do is only write to EEPROM if the ip or mac was changed (keeps your eeprom alive longer)

first test: Could you - Serial.println(name[0]); - after the parsing ? just to check the source-string is not changed.

second test: change - sscanf(value[1], "%u.%u.%u.%u", &ipAddr[0], &ipAddr[1], &ipAddr[2], &ipAddr[3]); into - sscanf(value[1], "%u.%u.%u", &ipAddr[0], &ipAddr[1], &ipAddr[2]);

I suspect %u, => it reads an unsigned int = 2 bytes, the second byte of ipAddr[0] will be overwritten with the first of ipAddr[1] etc, but the second byte of ipAddr[3] will overwrite macAddr[0] as these strings are declared side by side and will be side by side in memory.


1) After parsing name[0] is empty...

2) After that change mac is displayed correctly, so I think you're right...

How can I avoid this? What datatypes should I use?

1) After parsing name[0] is empty...

Could be also a side effect of the sscanf() writing a \0 on the first position of name[0];

test 3:


uint8_t ipAddr[4]; uint8_t macAddr[6];


int ipAddr[4]; int macAddr[6];

Takes twice the memory, but just to test it works correctly. If it does the error is understood 99% for sure.

If I use int, mac and ip are stored ok...

It will be ok if I use int and change readAddress and writeAddres to take int argument and pass int to and eeprom.write()?

It will be ok if I use int and change readAddress and writeAddres to take int argument and pass int to and eeprom.write()?

Takes 10 extra bytes. for storage. Alternative is to use a temporary array of ints to parse the fields and copy them in the uint8 array’s. But that code would take more that 10 bytes I guess.

We’re not done yet!

Test 4:
Could you go back to the original code and try %c instead of %u. %c reads a character and I guess it will do bytes as well.

%c converts to 49 7 16 8. I think that using int - it's Mega board so I have 4 KB of EEPROM and it should be enough to store everything I want...

So next question - when I want to store int using eeprom write should I change address in step by 2 (first value to address 0, second to 2, etc)?

check -

@robtillaart: I'll check that... Thanks for help and fast reaction... :)