有没有什么“标准”? C++ 中类似 htonl 的 64 位整数函数?
我正在研究 memcache 协议的实现,该协议在某些时候使用 64 位整数值。这些值必须以“网络字节顺序”存储。
我希望有一些 uint64_t htonll(uint64_t value)
函数来进行更改,但不幸的是,如果它存在,我找不到它。
所以我有 1 或 2 个问题:
- 是否有任何可移植(Windows、Linux、AIX)标准函数可以做到这一点?
- 如果没有这样的功能,你会如何实现呢?
我想到了一个基本的实现,但我不知道如何在编译时检查字节顺序以使代码可移植。因此,非常欢迎您的帮助;)
谢谢。
这是我写的最终解决方案,感谢布莱恩的解决方案。
uint64_t htonll(uint64_t value)
{
// The answer is 42
static const int num = 42;
// Check the endianness
if (*reinterpret_cast<const char*>(&num) == num)
{
const uint32_t high_part = htonl(static_cast<uint32_t>(value >> 32));
const uint32_t low_part = htonl(static_cast<uint32_t>(value & 0xFFFFFFFFLL));
return (static_cast<uint64_t>(low_part) << 32) | high_part;
} else
{
return value;
}
}
I'm working on an implementation of the memcache protocol which, at some points, uses 64 bits integer values. These values must be stored in "network byte order".
I wish there was some uint64_t htonll(uint64_t value)
function to do the change, but unfortunately, if it exist, I couldn't find it.
So I have 1 or 2 questions:
- Is there any portable (Windows, Linux, AIX) standard function to do this ?
- If there is no such function, how would you implement it ?
I have in mind a basic implementation but I don't know how to check the endianness at compile-time to make the code portable. So your help is more than welcome here ;)
Thank you.
Here is the final solution I wrote, thanks to Brian's solution.
uint64_t htonll(uint64_t value)
{
// The answer is 42
static const int num = 42;
// Check the endianness
if (*reinterpret_cast<const char*>(&num) == num)
{
const uint32_t high_part = htonl(static_cast<uint32_t>(value >> 32));
const uint32_t low_part = htonl(static_cast<uint32_t>(value & 0xFFFFFFFFLL));
return (static_cast<uint64_t>(low_part) << 32) | high_part;
} else
{
return value;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
减少“if num == ...”的开销
使用预处理器定义:
To reduce the overhead of the "if num == ..."
Use the pre-processor defines:
好吧,我认为最好在编译时尽可能使用字节序切换,但我更喜欢使用函数而不是宏,因为在宏中参数只是被实参替换;因此,如果参数在宏中多次出现(如前面提供的一些解决方案中所做的那样),则可以对参数进行多次求值并创建奇怪的结果。
因此,这允许调用 htonll(x++) 而无需多次增加 x,就像使用之前的宏一样。
Well, I think it's better to use the endian switching at compile time as much as possible, but I prefer to use function instead of macro, because in a macro the parameters are just replaced by the arguments; so arguments can be evaluated multiple times and create weird result if they are present multiple times in the macro (as done in some of the previous provided solutions).
So this allow calling htonll(x++) without increment x several time like it will be done using the previous macros.
我建议使用 endian.h
由于网络字节顺序按照惯例是大端字节序,因此您只需从主机调用适当的函数即可“be”。它包括 16、32 和 64 位版本。
我创建了以下单个 ntoh 模板化函数,它们适用于所有整数类型,因此我不必担心要调用哪个函数。
在 Linux 上,对该函数的调用没有任何损失,因为不需要进行任何转换。
在 Windows 上,谁关心 Windows ;-P
希望它能帮助别人。
I suggest to use the functions which are defined in endian.h
As network byte order is big endian by convention you just have to call the appropriate function from host to "be". It includes 16, 32 and 64 bits versions.
I've created the following single ntoh templated fuctions which work for all the integral types so I don't have to worry about which function to call.
On Linux, the call to that function has no penalty as no transformation need to be done.
On Windows, who cares about windows ;-P
Hope it helps someone.
编辑:将两者结合起来(使用布莱恩的代码):
警告:未经测试的代码!使用前请先测试。
EDIT: combining the two (used Brian's code):
Warning: untested code! Please test before using.
测试 (1==htonl(1)) 只是确定(遗憾的是在运行时)硬件架构是否需要字节交换。没有任何可移植的方法可以在编译时确定架构是什么,因此我们求助于使用“htonl”,它在这种情况下是可移植的。如果需要字节交换,那么我们使用 htonl 一次交换 32 位(记住也要交换两个 32 位字)。
这是执行交换的另一种方法,它可以跨大多数编译器和操作系统(包括 AIX、BSD、Linux 和 Solaris)移植。
重要的部分是使用
__BIG_ENDIAN__
或__LITTLE_ENDIAN__
;而不是__BYTE_ORDER__
、__ORDER_BIG_ENDIAN__
或__ORDER_LITTLE_ENDIAN__
。一些编译器和操作系统缺少 __BYTE_ORDER__ 和朋友。The test (1==htonl(1)) simply determines (at runtime sadly) if the hardware architecture requires byte swapping. There aren't any portable ways to determine at compile-time what the architecture is, so we resort to using "htonl", which is as portable as it gets in this situation. If byte-swapping is required, then we swap 32 bits at a time using htonl (remembering to swap the two 32 bit words as well).
Here's another way to perform the swap that is portable across most compilers and operating systems, including AIX, BSDs, Linux, and Solaris.
The important part is to use
__BIG_ENDIAN__
or__LITTLE_ENDIAN__
; and not__BYTE_ORDER__
,__ORDER_BIG_ENDIAN__
or__ORDER_LITTLE_ENDIAN__
. Some compilers and operating systems lack__BYTE_ORDER__
and friends.您可能正在寻找
bswap_64
我认为它几乎在所有地方都受支持,但我不会将其称为标准。您可以通过创建值为 1 的 int、将 int 的地址转换为 char* 并检查第一个字节的值来轻松检查字节顺序。
例如:
知道了这一点,您还可以创建一个简单的函数来进行交换。
您还可以始终使用包含可移植跨平台的字节序宏的 boost。
You are probably looking for
bswap_64
I think it is supported pretty much everywhere but I wouldn't call it standard.You can easily check the endianness by creating an int with a value of 1, casting your int's address as a
char*
and checking the value of the first byte.For example:
Knowing this you could also make a simple function that does the swapping.
You could also always use boost which contains endian macros which are portable cross platform.
您可以尝试使用
uint64_t htobe64(uint64_t host_64bits)
&uint64_t be64toh(uint64_t big_endian_64bits)
反之亦然。You can try with
uint64_t htobe64(uint64_t host_64bits)
&uint64_t be64toh(uint64_t big_endian_64bits)
for vice-versa.这似乎在 C 中可行;我做错了什么吗?
This seems to work in C; did I do anything wrong?