代码:
#include <stdlib.h>
#include <stdio.h>
unsigned char a2x(const char c)
{
switch(c)
{
case'0'...'9':
return (unsignedchar)atoi(&c);
case'a'...'f':
return 0xa+ (c-'a');
case'A'...'F':
return 0xa+ (c-'A');
default:
goto error;
}
error:
exit(0);
}
#defineMAC_LEN_IN_BYTE 6
#defineCOPY_STR2MAC(mac,str) \
do { \
for(int i = 0; i < MAC_LEN_IN_BYTE; i++) {\
mac[i] = (a2x(str[i*3]) << 4) + a2x(str[i*3 + 1]);\
}\
} while(0)
int main()
{
unsigned char macaddr[8] = {0};
char macaddr_str[18] ="8e:b2:cf:89:00:4a";
COPY_STR2MAC(macaddr,macaddr_str);
char mac_addr[30] = {'\0'};
sprintf(mac_addr,"%d.%d.%d.%d. %d.%d",macaddr[0],macaddr[1],macaddr[2],macaddr[3],macaddr[4],macaddr[5]);
printf("%s\n",mac_addr);
return0;
}
运行结果:
142.178.207.137.0.74
网友评论