2011-07-22 50 views
2

如何獲取:如何獲得ifr_hwaddr.sa_data燒焦

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]); 

到這個(可變店,不打印):

char mac[18]; 

感謝

回答

3

使用

#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

我需要將它保存到一個變量中,而不是打印它 – shaggy

+0

Sprintf(以s開頭)將格式化的輸出寫入char數組,而不是寫入stdout。看看stdio.h。你會發現printf,fprintf(將格式化的字符串寫入文件)和sprintf(將格式化的字符串寫入字符串)。試試我發佈的代碼。 –