我有下面的代码,但我无法将 int
放入我的 buffer
。
unsigned char buffer[1024];
buffer[0] = 0x44;
u_long fd = htonl(VERSION);
memcpy(buffer+1, &fd, 4);
你能帮帮我吗?
最佳答案
在 ntohl(buffer[1]);
中 buffer[1]
的值将被获取(一个字符)并将被转换为 long
。这将导致小端机器在 &buffer[1]
处取值,对于小于 224(unsigned) 的值显然为 0,正如您所调用的ntohl
前面的字节顺序颠倒。
如果你真的有兴趣查看缓冲区然后尝试
int *f =(int*) (&buffer[1]);
printf("\n %u \n", ntohl(*f));//Also *f but then you have to paste it in calculator to see allignment
关于c++ - 放入现有缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12770601/