码迷,mamicode.com
首页 > Web开发 > 详细

Network Endianness

时间:2016-02-09 15:51:32      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:

htons() Host to Network Short
htonl() Host to Network Long
ntohl() Network to Host Long
ntohs() Network to Host Short

When handling binary data transmitted or shared across platforms, you need be concerned with how each platform stores numerical values. A platform stores values either in big-endian or little-endian format. On big-endian machines, such as PowerPC machines, values are stored with the most-significant bytes first in memory; on little-endian machines, such as Pentium machines, values are stored with the least-significant bytes first. A multibyte value transmitted to a platform with a different format will be misinterpreted if it is not converted properly by one of the computers.

#include <sys/_endian.h>

#define ntohs(x)    __DARWIN_OSSwapInt16(x)
#define htons(x)    __DARWIN_OSSwapInt16(x)

#define ntohl(x)    __DARWIN_OSSwapInt32(x)
#define htonl(x)    __DARWIN_OSSwapInt32(x)

In Swift

CFSwapInt32()

instead of

ntonl() and ntohl()

and

CFSwapInt16()

instead of

ntons() and ntohs()

 

Reference:How do I set integer endianness using htonl in Swift?

Network Endianness

标签:

原文地址:http://www.cnblogs.com/xwjack1554239786/p/5185556.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!