1. 程式人生 > >C++ htons(), htonl(), ntohs(), ntohl()詳解

C++ htons(), htonl(), ntohs(), ntohl()詳解

將多位元組整數型別的資料,從主機的位元組順序轉化為網路位元組順序

#include <netinet/in.h>

uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);

htonl就是把本機位元組順序轉化為網路位元組順序。

所謂網路位元組順序(大尾順序)就是指一個數在記憶體中儲存的時候“高對低,低對高”(即一個數的高位位元組存放於低地址單元,低位位元組存放在高地址單元中)。但是計算機的記憶體儲存資料時有可能是大尾順序或者小尾順序。

h---host 本地主機
to  就是to 了
n  ---net 網路的意思
l 是 unsigned long

"s"表示short,"l"表示long