Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C htonl打印垃圾值_C_Sockets_Unix - Fatal编程技术网

C htonl打印垃圾值

C htonl打印垃圾值,c,sockets,unix,C,Sockets,Unix,变量“value”是uint32\u t value = htonl(value); printf("after htonl is %ld\n\n",value); This prints -201261056 value = htons(value); printf("after htons is %ld\n\n",value); This prints 62465 请说明原因?主机顺序是您的机器正确理解数据的顺序(假设您的机器

变量“value”是uint32\u t

    value = htonl(value);

    printf("after htonl  is %ld\n\n",value);

    This prints -201261056

    value = htons(value);

    printf("after htons  is %ld\n\n",value);

    This prints  62465

请说明原因?

主机顺序是您的机器正确理解数据的顺序(假设您的机器是little endian)。网络顺序是Big-Endian,您的系统无法正确理解它。这就是所谓垃圾值的原因

因此,基本上,代码中没有任何内容。:)

谷歌“Endianness”获取所有关于Big Endian和Little Endian的详细信息

提供更多信息,在大端,第一个字节或最低地址将有最高有效字节,在小端,在同一位置,最低有效字节将出现。因此,当您使用htonl时,您的第一个字节现在将包含最高有效字节,但您的系统将认为它是最低有效字节

考虑到维基百科中的十进制1000(十六进制3E8)的例子,大端将是03 E8,小端将是E8 03。现在,如果你把03个E8传递给一个小机器,它会被认为是十进制59395。
htonl()和htons()是用于将数据从主机端转换为网络端的函数

网络使用big-endian。因此,如果您的系统是X86,那么它就是little endian

主机到网络字节顺序(长数据)为htonl()。i、 e将32位值转换为网络字节顺序

主机到网络字节顺序(短数据)为htons()。i、 e将16位值转换为网络字节顺序

演示htonl()如何工作以及在htons()函数中使用32位值的效果的示例程序

#include <stdio.h>
#include <arpa/inet.h>

int main()
{
   long data = 0x12345678;
   printf("\n After htonl():0x%x  , 0x%x\n", htonl(data), htons(data));
   return 0;
}
#包括
#包括


我猜你的输入是500,不是吗

500是2*8+2*7+2*6+2*5+2*4+2*2或
0x00 0x00 0x01 0xF4
的小尾端顺序

TCP/IP使用big-endian。因此,在htonl之后,序列是
0xF4 0x01 0x00 0x00

如果将其打印为有符号整数,因为第一个数字是1,所以它是负数。负数被视为补码,其值为-(2*27+2*25+2*24+2*23+2*22+2*21+2*20+2*19+2*18+2*17+2**16)==-201261056

@halfelf>我只是想把我的发现放在这里。我刚刚用同样的方法尝试了下面的程序
@halfelf>I jut want to put my findings.I just tried the below program with the same 
value 500.I guess you have mistakenely mentioned output of LE as BE and vice versa
Actual output i got is 0xf4 0x01 0x00 0x00 as little Endian format.My Machine is 
LE
#include <stdio.h>
#include <netinet/in.h>
/* function to show bytes in memory, from location start to start+n*/
void show_mem_rep(char *start, int n)
{
    int i;
     for (i = 0; i < n; i++)
      printf(" %.2x-%p", start[i],start+i);
     printf("\n");
}

/*Main function to call above function for 0x01234567*/
int main()
{
  int i = 500;//0x01234567;
   int y=htonl(i); --->(1)
   printf("i--%d , y---%d,ntohl(y):%d\n",i,y,ntohl(ntohl(y)));
   printf("_------LITTLE ENDIAN-------\n");
   show_mem_rep((char *)&i, sizeof(i));
   printf("-----BIG ENDIAN-----\n");/* i just used int y=htonl(i)(-1) for reversing 
   500 ,so that
   i can print as if am using a BE machine. */

   show_mem_rep((char *)&y, sizeof(i));

   getchar();
   return 0;
  }
output is 
i--500 , y----201261056,ntohl(y):-201261056
_------LITTLE ENDIAN-------
 f4-0xbfda8f9c 01-0xbfda8f9d 00-0xbfda8f9e 00-0xbfda8f9f
 -----BIG ENDIAN-----
  00-0xbfda8f98 00-0xbfda8f99 01-0xbfda8f9a f4-0xbfda8f9b
值500。我猜您错误地将LE的输出称为BE,反之亦然 我得到的实际输出是0xf4 0x01 0x00 0x00小端格式。我的机器是 乐 #包括 #包括 /*函数显示内存中的字节,从位置开始到开始+n*/ 无效显示成员代表(字符*开始,整数n) { int i; 对于(i=0;i(1) printf(“i--%d,y--%d,ntohl(y):%d\n”,i,y,ntohl(ntohl(y)); printf(“————————————\n”); show_mem_rep((char*)&i,sizeof(i)); printf(“----BIG-ENDIAN-----\n”);/*我刚刚使用int y=htonl(i)-1进行反转 500,所以 我可以像使用BE机器一样打印*/ 显示成员代表((字符*)&y,大小(i)); getchar(); 返回0; } 输出为 i--500,y--201261056,ntohl(y):-201261056 _------小恩迪亚------- f4-0xbfda8f9c 01-0xbfda8f9d 00-0xbfda8f9e 00-0xbfda8f9f -----大端----- 00-0xbfda8f98 00-0xbfda8f99 01-0xbfda8f9a f4-0xbfda8f9b
否,主机订单指主机订单。不是小端或大端,而是主机使用的任何顺序。它可能是小endian,也可能是big endian,如果你在一个时间机器中,或者其他什么东西中,它可能是。@放松,抱歉。我认为你是对的。我修改了我的答案以反映这一点。但是,我想答案仍然与眼前的问题有关。如果你仍然觉得我的答案有错误,请随意指出或修改。谢谢。:)您使用了错误的格式说明符,
%ld
用于打印
带符号的long
,在许多系统中,这将是8字节类型。包括
stdint.h
并使用
PRIu32
作为
uint32\u t