如何将单个字节放入网络字节顺序?

umd*_*der 2 c byte network-programming network-protocols bit

所以我有变量:

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;
Run Code Online (Sandbox Code Playgroud)

我想按网络字节顺序排列所有值。

我知道用于htons()16 位值和htonl()32 位值。

所以我会这样做:uint32_t source_address = htonl(0);并且uint16_t checksum = htons(0);

8 位值应该使用什么?

Cor*_*lks 5

8 位值应该使用什么?

没有什么。1 字节(8 位)值没有“字节序”。所以你不必担心它们(这就是为什么它们没有hton功能)。