小尾数法中的ntohl()vs htonl() [英] ntohl() vs htonl() in Little Endian

查看:127
本文介绍了小尾数法中的ntohl()vs htonl()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请澄清我的疑问,因为我对以下内容感到非常困惑,并且在网上的其他任何地方都无法获得
干净的答案。

 #include< stdio.h> 
int main()
{
int a = 0x44332211;
printf( Network-0x%x\n,htonl(a)); //主机到网络
printf( Host-0x%x\n,ntohl(a)) ; //托管
的网络返回0;
}

输出:

 网络-0x11223344 
主机-0x11223344

此处 htonl(0x44332211) =>我正在将little endian(LE)转换为BE。因此输出将为 0x11223344 。我明白了。我的问题是 ntoh()。现在 ntohl(0x44332211) =>什么?



这里我正在1个终端上执行这两个命令。因此,主机到网络,即 hton()表示我的网络终端。这就说得通了。但是在这里 ntohl()是什么意思? ntohl()出现在以下情况中:

  a PC A ----(即hton)通过网络发送数据------>(即ntoh)到PC B? 

ntoh 期望网络字节顺序,即大端。请解释一下 ntohl()的含义,以及为什么其打印内容与 0x11223344 相同,为什么不 0x44332211

解决方案

假设您的程序在低端字节序的计算机(例如x86)上运行, nphl(a)将返回0x11223344(即,它将翻转您的0x11223344值的字节顺序,就像htonl()一样)。



程序在大型字节序计算机(例如PowerPC Mac)上运行,则ntohl(a)和htonl(a)都将逐字返回a,因为该计算机的内部/主机格式已经与网络格式相同。 / p>

还有(至少在理论上)您的程序在某些异常硬件,它使用既不是big-endian也不是little-endian的数字表示形式。在那种情况下,针对该环境的htonl()和ntohl()函数将被编程为执行正确的操作(无论可能是什么),以将该计算机的内部表示形式转换为标准网络表示形式(big-endian),并且



这些功能的预期使用模式是:




  • 任何时候您将要向网络发送长消息,在其上调用htonl()并发送该值。

  • 任何时候只要您从网络上收到了很长时间,都可以在其上调用ntohl()并改用该值。



这样,主机的数字表示形式和网络/大端数字表示形式之间的任何差异都将在htonl()中自动为您处理和ntohl(),您不必担心为程序可能运行的每种计算机体系结构编写(和测试!)单独的转换代码。


Kindly clarify my doubt as i got so confused with the below stuff and i couldnt get a clean anwser any where else on net.

#include<stdio.h>
int main()
{
   int a = 0x44332211;
   printf("Network - 0x%x\n", htonl(a));// Host to network
   printf("Host    - 0x%x\n", ntohl(a));// Network to host
   return 0;
}

Output:

 Network - 0x11223344   
 Host    - 0x11223344  

Here htonl(0x44332211) => i am converting little endian(LE) to BE. So output will be 0x11223344. That i understood. My problem is with ntoh(). Now ntohl(0x44332211) => what?

Here i am executing both the commands on 1 terminal. So host to network, ie hton() means my terminal to network. That makes sense. But here ntohl() means what? ntohl() comes into picture if we have:

a PC A----(ie hton)sending data over network------>(ie ntoh) to PC B?

Also ntoh expects a network byte order ie Big endian. Kindly intepret what ntohl() means above and why its printed same as 0x11223344 and why not 0x44332211?

解决方案

Assuming your program is running on a little-endian machine (e.g. x86), ntohl(a) will return 0x11223344 (i.e. it will flip the byte ordering of your 0x11223344 value, just like htonl() does).

On the other hand, if your program is running on a big-endian machine (e.g. a PowerPC Mac), then ntohl(a) and htonl(a) will both return a, verbatim, since the machine's internal/host format is already the same as the network format.

There's also the (at least theoretical) possibility that your program is running on some unusual hardware that uses a numeric representation that is neither big-endian nor little-endian. In that case, the htonl() and ntohl() functions for that environment would have been programmed to do the right thing (whatever that might be) to convert that machine's internal representation to the standard network representation (which is big-endian) and back.

The expected usage pattern for these function is this:

  • Anytime you are about to send a long to the network, call htonl() on it and send that value instead.
  • Anytime you have just received a long from the network, call ntohl() on it and use that value instead.

That way any variances between the host computer's numeric representation and the network/big-endian representation are automatically handled for you inside htonl() and ntohl(), and you don't have to worry about writing (and testing!) separate conversion code for every computer architecture your program might possibly ever run on.

这篇关于小尾数法中的ntohl()vs htonl()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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