Вы можете использовать htonl()
, htons()
, ntohl()
и ntohs()
, чтобы сделать его байтовым.
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
Пример:
- (void)testExample {
UInt32 length = 0x1a2b3c4d;
NSLog(@"%x", length);
length = htonl(length);
NSLog(@"%x", length);
NSMutableData *data = [[NSMutableData alloc] init];
[data appendBytes:&length length:4];
NSLog(@"%@", data);
}
print
2015-10-29 15:46:49.224 UPHTTP-iOS[3896:101301] 1a2b3c4d
2015-10-29 15:46:49.224 UPHTTP-iOS[3896:101301] 4d3c2b1a
2015-10-29 15:46:49.224 UPHTTP-iOS[3896:101301] <1a2b3c4d>