BIG_ENDIAN代码也要修复LITTLE_ENDIAN机器
BIG_ENDIAN code to be fixed for LITTLE_ENDIAN machine as well
我有这段遗留代码,我需要在BIG &小小的Endian机器。问题在于hton()。
msg->Mac是char Mac[16+1]现有代码:(仅适用于BIG)
if (sscanf(msg->Mac, "%4hx.%4hx.%4hx", (unsigned short *)&new_mac[0],
(unsigned short *)&new_mac[2],
(unsigned short *)&new_mac[4]) != 3) {
return (ERROR_ADDRESS_TRANSLATION);
}
*(unsigned short *)&new_mac[0] = hton(*(unsigned short *)&new_mac[0]);
*(unsigned short *)&new_mac[2] = hton(*(unsigned short *)&new_mac[2]);
*(unsigned short *)&new_mac[4] = hton(*(unsigned short *)&new_mac[4]);
sprintf((char *)newMac, "%04x.%04x.%04x", *(unsigned short *)&new_mac[0],
*(unsigned short *)&new_mac[2], *(unsigned short *)&new_mac[4]);
/* Get the MAC address */
if (sscanf((char *)newMac, "%4hx.%4hx.%4hx", (unsigned short *)&mac_addr[0],
(unsigned short *)&mac_addr[2],
(unsigned short *)&mac_addr[4]) != 3) {
return (ERROR_ADDRESS_TRANSLATION);
}
/* Convert to network order */
*(unsigned short *)&mac_addr[0] = hton(*(unsigned short *)&mac_addr[0]);
*(unsigned short *)&mac_addr[2] = hton(*(unsigned short *)&mac_addr[2]);
*(unsigned short *)&mac_addr[4] = hton(*(unsigned short *)&mac_addr[4]);
为了在LITTLE Endian机器上修复这个问题,我使用了一个SWAP宏,它将在short内交换字节。这是正确的方法吗?
我在上面添加的代码:(使它在LITTLE上也能工作)
#if __BYTE_ORDER != __BIG_ENDIAN
*(unsigned short *)&mac_addr[0] = SWAP(*(unsigned short *)&mac_addr[0]);
*(unsigned short *)&mac_addr[2] = SWAP(*(unsigned short *)&mac_addr[2]);
*(unsigned short *)&mac_addr[4] = SWAP(*(unsigned short *)&mac_addr[4]);
#endif
你在小端机上测试了吗?因为hton已经考虑了arch字节:
/* Copyright (C) 1993-2015 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
#include <stdint.h>
#include <netinet/in.h>
#undef htonl
#undef ntohl
uint32_t
htonl (uint32_t x)
{
#if BYTE_ORDER == BIG_ENDIAN
return x;
#elif BYTE_ORDER == LITTLE_ENDIAN
return __bswap_32 (x);
#else
# error "What kind of system is this?"
#endif
}
weak_alias (htonl, ntohl)
特别针对MAC地址:它们应该被视为一个6字节的数组,所以应该没有端序问题。试图映射其他类型(为了效率?)只是在自找麻烦。
所以scanf应该是:
sscanf(msg->Mac, "%2hhx%2hhx.%2hhx%2hhx.%2hhx%2hhx",
&new_mac[0], &new_mac[1],
&new_mac[2], &new_mac[3],
&new_mac[4], &new_mac[5])
相关文章:
- 从值小于256的uint16到uint8的Endian安全转换
- 如何从Little Endian UTF-16编码字节中获取C++std::string
- 如何在C 中的Little Endian中读取来自二进制文件的单独的十六进制字符
- 正在使用Boost Endian算术类型被认为是好实践
- 操作方法:将 boost::endian 缓冲区类型转换回本机格式
- 如何将Little-endian 64转换为C 中的主字节订单
- 如何从c 中的文件中读取小endian整数
- endian.h on Windows
- 将Integer转换为Little Endian十六进制字符串
- 将double转换为LSBF(小Endian)十六进制字符串
- c/c++中endian的最佳可移植转换
- 如何以可移植的方式将integer转换为little-endian
- 在c++中,什么是endian转换的最佳样式
- C++将double序列化为little-endian中的二进制文件
- big-endian中的整数强制转换
- 如何在C++中从字节数组(在 BIG-ENDIAN 中)中提取单个字段
- 将数据转换为big-endian
- big和little endian浮点之间的区别是什么
- 在C++中加载位图时出现Big Endian问题
- Big Endian字节与字符串作为字符串数据库中的键