Conversion of Ethernet address to readable form?

前端 未结 3 594
鱼传尺愫
鱼传尺愫 2021-01-27 03:33
struct ethernet_header
{
    u_char ether_dhost[ ETHER_ADDR_LEN];

    u_char ether_shost[ETHER_ADDR_LEN];

    u_short ether_type;
};

for(i = 0;i <6; i++)
  printf(         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-27 03:48

    Something like this should do it:

    char mac[6 * 2 + 5 + 1];
    
    for(size_t i = 0, pos = 0; i < sizeof ethernet->ether_dhost; i++)
    {
      if(i > 0)
       mac[pos++] = ':';
      sprintf(mac + pos, "%02x", (unsigned int) ethernet->ether_dhost[i] & 0xffu);
    }
    

    This also inserts colons between each byte, so the output will look like DE:AD:BE:EF:BA:BE which is how MAC addresses are commonly formatted for Ethernet.

提交回复
热议问题