问题描述
我找到了以下函数从,它将主机字节序中的 unsigned long
从网络字节转换为 unsigned long
ie在小端定义为:
I have found the following function from MSDN which converts an unsigned long
from network byte to unsigned long
in host byte order i.e. in little-endian defined as:
u_long WSAAPI ntohl(
_In_ u_long netlong
);
MSDN文档说它可以转换32位数字。但是因为在C ++中,我已经读过 long
和 int
不一样ie long
不保证为32位或整数的相同大小 INT_MAX
。
The MSDN document says that it can convert a 32 bits number. But since in C++ as I have read that long
and int
are not the same i.e. long
is not guaranteed to be 32 bits or the same size of an integer INT_MAX
.
所以,我不知道是否有一个类似的函数,它需要一个32位的值,如 unsigned int
,而不是 unsigned long
?
So, I wonder if there is a a similar function which takes a 32 bits values such as unsigned int
instead of unsigned long
?
推荐答案
特别指出 ntohl
的 netlong
参数是一个32位值:
The documentation specifically says that ntohl
's netlong
parameter is a 32-bit value:
TCP / IP网络字节顺序中的32位数。
您是对的 - 在标准C ++ 中, long
为任何特定大小,但它必须至少
You're right -- in Standard C++ a long
is not guaranteed to be any particular size, except that it must be at least 32 bits.
然而,由于我们在谈论endian转换函数,所以我们谈论平台特定这里。现在我们需要深入了解 long
是在Windows 下的。在Windows下,长时间的:
However since we're talking about the endian conversion functions, we're talking about platform-specifics here. We need to drill down now in to what a long
is under Windows. And under Windows, a long is 32-bits:
typedef long LONG;
这篇关于将网络字节顺序(大端)转换为小端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!