MAC 地址解析
MAC address parsing
我有一个像"6F:e:5B:7C:b:a"这样的MAC地址,我想解析并在:e:,:b:,:a之前插入隐式零。
我目前无法使用 Boost,但我有一个粗略的解决方案。解决方案在":"上拆分。然后我计算字符之间的字符,如果只有一个,我会在前面插入一个零。
我想知道是否有人有更快的方法?
对于快速和肮脏:
if (sscanf(text, "%x:%x:%x:%x:%x:%x",
&mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]) != 6) {
// handle error
}
请注意,它不会检查数字是否真的是十六进制。sscanf() 的常规预防措施适用。
首先,
您可以使用脚本将char
转换为int
非常快,因此:
unsigned char hex_to_int(const char c)
{
if( c >= 'a' && c <= 'f'){
return c - 'a' + 10;
}
if( c >= 'A' && c <= 'F'){
return c - 'A' + 10;
}
if( c >= '0' && c <= '9'){
return c - '0';
}
return 0;
}
然后,您可以创建循环来迭代字符串:
unsigned char mac[6]; /* Resulting mac */
int i; /* Iteration number */
char *buffer; /* Text input - will be changed! */
unsigned char tmp; /* Iteration variable */
for( i = 0; i < 6; ++i){
mac[i] = 0;
/*
* Next separator or end of string
* You may also want to limit this loop to just 2 iterations
*/
while( ((*buffer) != ' ') && ((*buffer) != ':'){
mac[i] <<= 4;
mac[i] |= hex_to_int( *buffer);
++buffer;
}
}
if( (i != 6) || (*buffer != NULL)){
// Error in parsing, failed to get to the 6th iteration
// or having trailing characters at the end of MAC
}
此函数不执行任何错误检查,但它可能是您将获得的最快解决方案。
相关文章:
- 动态获取 esp32 的 mac 地址并在以太网库中使用它.
- 将Wiimote MAC地址转换为PIN码,从C++转换为Python或Javascript
- 读取 MAC 地址时文件读取异常
- OMNET++:如何在INET 4.0中获取帧的源MAC地址?
- 错误:语句无法解析重载函数C++的地址
- "cin"错误地解析了Mac上的"double"输入
- 如何在解析过程中区分 IP 地址和域名
- 语句无法解析过载函数C 指标的地址
- Boost JSON解析器和IP地址
- 获得MAC地址的代码似乎会损坏内存,但仅在发布构建中而不是调试构建
- 如何在 Windows 中使用 C++ 获取 MAC 地址
- MAC 地址解析
- Boost Regex用于格式化MAC地址字符串
- 从Netsh WLAN获取MAC地址
- Linux C++ 如何以编程方式获取 LAN 上所有适配器的 MAC 地址
- 将格式化的 mac 地址写入字符串流
- 如何在给定设备实例ID的情况下可靠快速地获取网卡的MAC地址
- 用于in_addr结构(IPv4地址)的Mac/Windows不同格式
- Boost Spirit电子邮件地址解析器属性生成
- Ip地址解析使用Mac地址编程