将网络字节顺序(大端)转换为小端 [英] Converting network byte order (big endian) to little endian

查看:974
本文介绍了将网络字节顺序(大端)转换为小端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了以下函数从 MSDN ,它将主机字节序中的 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位数。

A 32-bit number in TCP/IP network byte order.








我读到long和int不一样,即长不是
保证是32位或整数INT_MAX的相同大小。

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.

您是对的 - 在标准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下,长时间的是32位

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:


有符号整数。范围是-2147483648到2147483647
十进制。这种类型在WinNT.h中声明如下:

A 32-bit signed integer. The range is –2147483648 through 2147483647 decimal. This type is declared in WinNT.h as follows:



typedef long LONG;

这篇关于将网络字节顺序(大端)转换为小端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆