【发布时间】:2019-04-16 02:30:48
【问题描述】:
我创建一个演示来证明网络字节顺序与大端相同:
#include "stdio.h"
#include "stdint.h"
#include "winsock2.h"
#define BL32(x) ((((x) & 0x000000ffUL) << 24) | \
(((x) & 0x0000ff00UL) << 8) | \
(((x) & 0x00ff0000UL) >> 8) | \
(((x) & 0xff000000UL) >> 24))
int main(int argc, char* argv[])
{
uint32_t s = INT_MAX; // little endian
uint32_t network = htonl(s);
uint32_t bigendian = BL32(s);
if(network == bigendian) {
printf("nbo is same as big endian\n");
} else {
printf("nbo isn't same as big endian\n");
}
return 0;
}
这个程序在 x86(litte endian) Windows PC 上运行,它会给出输出:
nbo is same as big endian
我没有看到教科书或教程中提到这一点,所以我想确认它是否正确。
顺便说一句,我认为帮助理解网络中的字节顺序非常重要。为什么大多数问题只集中在“大端与小端”...
【问题讨论】:
标签: c sockets network-programming