如何Mac的字符串转换为在C字节地址 [英] How to convert Mac string to a Byte address in C

查看:194
本文介绍了如何Mac的字符串转换为在C字节地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要带命令行的MAC地址,所以我把它作为字符串...我怎么可以在这个17字节的MAC字符串转换,如00:0D:3F:CD:02:5F,以6字节的MAC在C类地址

I want to take MAC address from command line, so I got it as string...how do I can convert this 17 byte MAC string like "00:0d:3f:cd:02:5f" to 6 byte MAC Address in C

推荐答案

在一个C99-要求的实现,这应该工作

On a C99-conformant implementation, this should work

unsigned char mac[6];

sscanf(macStr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]);

否则,你将需要:

Otherwise, you'll need:

unsigned int iMac[6];
unsigned char mac[6];
int i;

sscanf(macStr, "%x:%x:%x:%x:%x:%x", &iMac[0], &iMac[1], &iMac[2], &iMac[3], &iMac[4], &iMac[5]);
for(i=0;i<6;i++)
    mac[i] = (unsigned char)iMac[i];

这篇关于如何Mac的字符串转换为在C字节地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆