我在主机(取决于 CPU)和网络(大端)之间转换字节顺序时遇到问题。这些是我发现的所有 API(在 Linux 的“arpa/inet.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);
除了一件事,它们只处理无符号整数(2 字节或 4 字节)。
那么有什么方法可以处理有符号整数大小写吗?换句话说,如何实现以下功能(API)?
int32_t htonl(int32_t hostlong);
int16_t htons(int16_t hostshort);
int32_t ntohl(int32_t netlong);
int16_t ntohs(int16_t netshort);