#include <stdio.h> #include <string.h> #define unsigned int u32 #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x" #define MACSTOL(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5] #define MACLTOS(a) (u32 *)&(a)[0], (u32 *)&(a)[1], (u32 *)&(a)[2], (u32 *)&(a)[3], (u32 *)&(a)[4], (u32 *)&(a)[5] int main(int argc,char argv[]) { char mac_add[18] = "00:12:32:4a:56:7b"; char mac_add_tmp[6] = {0}; char mac_add_test[18] = {0}; /*用sscanf将18字节转为6字节,有的编译器可以不用(u32 *)进行强转*/ sscanf(mac_add, MACSTR, MACLTOS(mac_add_tmp)); /*6字节转为18字节*/ snprintf(mac_add_test, 18, MACSTR, MACSTOL(mac_add)); printf("original : %s new : %s\n", mac_add, mac_add_test); return 0; }
时间: 2024-10-10 01:47:31