Network Endianness

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Network Endianness相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

NETWORK_PROVIDER 和 GPS_PROVIDER 在片段中返回 null

oracle的tts迁移

前端防扒代码片段

前端防扒代码片段

前端防扒代码片段

前端防扒代码片段