我有一个问题需要在主机(依赖于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);