所以我有变量:
uint8_t version = 1;
uint8_t ttl = 1;
uint16_t payload_length =
uint32_t account_identifier = 24027;
uint32_t source_address = 0;
uint32_t destination_address = 0;
uint16_t checksum = 0;
uint16_t protocol = 1;
我想按网络字节顺序生成所有值。
我知道对 16 位值使用 htons()
,对 32 位值使用 htonl()
。
所以我会这样做:uint32_t source_address = htonl(0);
和 uint16_t checksum = htons(0);
我应该为 8 位值使用什么?
最佳答案
What should I use for the 8 bit values?
没有。 1 字节(8 位)值没有“字节顺序”。所以您不必担心它们(这就是为什么没有针对它们的 hton
函数)。
关于c - 如何将单个字节放入网络字节顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26475542/