开发者

How to get ifr_hwaddr.sa_data to char

How to get this:

printf(" %02x:%02x:%02x:%02x:%02x:%02x",
(unsigned char)item->ifr_hwaddr.sa_data开发者_JAVA技巧[0],
(unsigned char)item->ifr_hwaddr.sa_data[1],
(unsigned char)item->ifr_hwaddr.sa_data[2],
(unsigned char)item->ifr_hwaddr.sa_data[3],
(unsigned char)item->ifr_hwaddr.sa_data[4],
(unsigned char)item->ifr_hwaddr.sa_data[5]);

into this(store in variable, not print it):

char mac[18];

Thanks


Use

#include <stdio.h>

int main(int argc, char **argv)
{
    char mac[19]; /* one extra for terminating '\0'; 
                     You may want to make it a little larger
                     still, just to be sure. */

    sprintf(mac, " %02x:%02x:%02x:%02x:%02x:%02x", 
        (unsigned char)item->ifr_hwaddr.sa_data[0],
        (unsigned char)item->ifr_hwaddr.sa_data[1],
        (unsigned char)item->ifr_hwaddr.sa_data[2],
        (unsigned char)item->ifr_hwaddr.sa_data[3],
        (unsigned char)item->ifr_hwaddr.sa_data[4],
        (unsigned char)item->ifr_hwaddr.sa_data[5]);

    printf("%s\n", mac); // string was "printed" to mac array!

    return 0;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜