您的位置:首页 > 理论基础 > 计算机网络

网络大小端转换函数

2016-08-26 15:35 302 查看

网络大小端转换函数

//*****************************************************************************
//
// htonl/ntohl - big endian/little endian byte swapping macros for
// 32-bit (long) values
//
//*****************************************************************************
#ifndef htonl
static uint32_t
htonl(uint32_t a)
{
return ((a >> 24) & 0x000000ff) |
((a >>  8) & 0x0000ff00) |
((a <<  8) & 0x00ff0000) |
((a << 24) & 0xff000000);
}
#endif

#ifndef ntohl
#define ntohl(a)    htonl((a))
#endif

//*****************************************************************************
//
// htons/ntohs - big endian/little endian byte swapping macros for
// 16-bit (short) values
//
//*****************************************************************************
#ifndef htons
static uint16_t
htons(uint16_t a)
{
return ((a >> 8) & 0x00ff) | ((a << 8) & 0xff00);
}
#endif

#ifndef ntohs
#define ntohs(a)    htons((a))
#endif
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  函数 网络 htonl ntohl