MAC地址与字符串的相互转化
MAC地址转字符串,直接使用宏
#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
#define MACSTR "%02X:%02X:%02X:%02X:%02X:%02X"//转换后的格式,可按需求修改
//使用
char mac[6];//mac
char mac_str[15] = {0};
sprintf(mac_str, MYMACSTR, MAC2STR(mac));
字符串获取mac地址:
static int hex2num(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return -1;
}
void hexstr2mac(char *dst, char *src) {
int i=0;
while(i<6) {
if(' ' == *src||':'== *src||'"'== *src||'\''== *src) {
src++;
continue;
}
*(dst+i) = ((hex2num(*src)<<4)|hex2num(*(src+1)));
i++;
src += 2;
}
}
//使用
char mac[6]={0};
char* macstr="AA:BB:CC:DD:EE:FF";
hexstr2mac(&mac,macstr);