Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get ifr_hwaddr.sa_data to char

Tags:

c

How to get this:

printf(" %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]);

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

char mac[18];

Thanks

like image 334
shaggy Avatar asked Jul 22 '11 15:07

shaggy


1 Answers

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;
}
like image 181
Rudy Velthuis Avatar answered Oct 21 '22 12:10

Rudy Velthuis