htonl 是网络编程(BSD sockets)中常见的函数/宏名,表示 “Host TO Network Long”:把主机字节序(host byte order)的 32 位无符号整数转换为网络字节序(network byte order,通常为大端序)。常用于在发送 IP 地址、端口之外的 32 位字段前进行字节序转换。
(相关的还有 htons:16 位;以及 ntohl/ntohs:从网络字节序转回主机字节序。)
/ˌeɪtʃ tiː oʊ ˈɛn ɛl/
I used htonl to convert the 32-bit value before sending it over the socket.
我在通过套接字发送之前用 htonl 把这个 32 位数值转换成网络字节序。
When constructing a custom protocol header, the code writes the length field as htonl(payload_length) to ensure consistent interpretation across different CPU architectures.
在构造自定义协议头时,代码把长度字段写成 htonl(payload_length),以确保在不同 CPU 架构上都能一致地解析。
htonl 是缩写组合:h(host,主机) + to(到) + n(network,网络) + l(long,long 类型/32 位“长整型”概念)。它起源于早期 Unix/BSD 的网络接口设计,用于解决不同机器的字节序差异,使网络传输的数据在各平台间可互操作。