作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这段遗留代码,我需要在 BIG 和 LITTLE 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 宏,它可以在短时间内交换字节。这是正确的方法吗?
我在上面添加的代码:(使其也适用于 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 已经考虑了拱字节性:
/* 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)
关于c++ - LITTLE_ENDIAN 机器也将修复 BIG_ENDIAN 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31723227/
我有这段遗留代码,我需要在 BIG 和 LITTLE Endian 机器上运行。问题出在 hton() 上。 msg->Mac 是 char Mac[16+1]现有代码:(仅适用于 BIG) if (
我正在试验 C++,我决定尝试 is_big_endian 代码,就像我在 C 中做的一样。但是,当我尝试打印出指针的值时,我没有得到任何输出。我尝试了 C 和 C++ 风格的转换。我做错了什么? #
如果我需要以不同方式处理 LITTLE_ENDIAN 和 BIG_ENDIAN 情况,如果我的系统是这样,我该如何测试 BIG_ENDIAN 情况使用LITTLE_ENDIAN?当系统的ByteOrd
我是一名优秀的程序员,十分优秀!