socket 数据高低位转换

int length = (int)[data length];
//将主机数转换成无符号长整型的网络[字节顺序]
HTONL(length);
int i = 1;
NSData *data = [NSData dataWithBytes: &i length: sizeof(i)];

int i;
[data getBytes: &i length: sizeof(i)];
htonl()--"Host to Network Long int" 32Bytes 
ntohl()--"Network to Host Long int" 32Bytes 
htons()--"Host to Network Short int" 16Bytes 
ntohs()--"Network to Host Short int" 16Bytes
#define NTOHL(x) (x) = ntohl((__uint32_t)x)
#define NTOHS(x) (x) = ntohs((__uint16_t)x)
#define NTOHLL(x) (x) = ntohll((__uint64_t)x)
#define HTONL(x) (x) = htonl((__uint32_t)x)
#define HTONS(x) (x) = htons((__uint16_t)x)
#define HTONLL(x) (x) = htonll((__uint64_t)x)

你可能感兴趣的:(socket 数据高低位转换)