tech_log: ether_ntoa() format

Wednesday, August 5, 2009

ether_ntoa() format

ether_ntoa_r.c - glibc-2.3.5/inet :
char *
ether_ntoa_r (const struct ether_addr *addr, char *buf)
{
  sprintf (buf, "%x:%x:%x:%x:%x:%x",
           addr->ether_addr_octet[0], addr->ether_addr_octet[1],
           addr->ether_addr_octet[2], addr->ether_addr_octet[3],
           addr->ether_addr_octet[4], addr->ether_addr_octet[5]);
  return buf;
}


That means that if we have some of the bytes like 5, or 0, we can get an address like this :
5:3a:31:0:34:1c

This is not adequate if we want to be sure that value has been printed always with two nubers between ':'.

That's why we can a coriged solution :
sprintf (mac, "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
           ea.ether_addr_octet[0], ea.ether_addr_octet[1],
           ea.ether_addr_octet[2], ea.ether_addr_octet[3],
           ea.ether_addr_octet[4], ea.ether_addr_octet[5]);



0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home