字节序大小端转换、模拟htons、htonl、ntohs、ntohl
来源:互联网 发布:127.0.0.1 linux 编辑:程序博客网 时间:2024/05/19 04:04
大端模式:一个多字节数据的高字节在前,低字节在后,以数据 0x1234ABCD 看例子: 低地址 ---------------------> 高地址 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 12 | 34 | AB | CD | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+小端模式:一个多字节数据的低字节在前,高字节在后,仍以 0x1234ABCD 看: 低地址 ---------------------> 高地址 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- | CD | AB | 34 | 12 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
typedef unsigned short int uint16;typedef unsigned long int uint32;#define BigLittleSwap16(A) ((((uint16)(A) & 0xff00)>>8)|\ (((uint16)(A) & 0x00ff)<<8))#define BigLittleSwap32(A) (((uint32)(A) & 0xff000000) >> 24)|\ (((uint32)(A) & 0x00ff0000) >> 8)|\ (((uint32)(A) & 0x0000ff00) << 8)|\ (((uint32)(A) & 0x000000ff) << 24)
//本机大端返回true,小端返回falsebool checkCPUendian(){ union { unsigned long int i; unsigned char s[4]; }c; c.i = 0x12345678; return (0x12 == c.s[4]);}
// 模拟htonl 本机字节序转网络字节序// 若本机为大端,与网络字节序相同,直接返回// 若本机为小端,转换成大端再返回unsigned long int htonl(unsigned long int h){ return checkCPUendian() ? h : BigLittleSwap32(h);}unsigned long int ntohl(unsigned long int n){ return checkCPUendian() ? n : BigLittleSwap32(n);}unsigned short int htons(unsigned short int h){ return checkCPUendian() ? h : BigLittleSwap16(h);}unsigned short int ntohs(unsigned short int n){ return checkCPUendian() ? n : BigLittleSwap16(n);}
阅读全文
0 0
- 字节序大小端转换、模拟htons、htonl、ntohs、ntohl
- 大小端转换htonl、ntohl、htons、ntohs
- htons ntohs htonl ntohl 大小端字节变换
- 高低字节序转换(htonl、ntohl、htons、ntohs函数)
- 网络字节转换函数htons htonl ntohs ntohl
- 网络字节转换函数 htons(), htonl(), ntohs(), ntohl()
- 网络字节序与主机字节序的转换(htonl,htons,inet_addr,ntohl,ntohs,inet_ntoa)
- 主机字节序与网络字节序的转换函数:htonl、ntohl、htons、ntohs
- 主机字节序与网络字节序的转换函数:htonl、ntohl、htons、ntohs
- 主机字节序与网络字节序的转换函数:htonl、ntohl、htons、ntohs
- 模拟htonl、ntohl、htons、ntohs函数实现
- htons(), htonl(), ntohs(), ntohl()
- ntohs(),ntohl(),htons(),htonl(),
- htons(), htonl(), ntohs(), ntohl()
- htons(), htonl(), ntohs(), ntohl()
- htons,htonl,ntohs,ntohl
- htons(), htonl(), ntohs(), ntohl()
- htonl(), ntohl(), htons(), ntohs().
- 为什么会需要消息队列(MQ)?
- 征集SQL,大家集思广益下
- 小程序文档整理之 -- API(开放接口)
- Quartz学习之二-CronTrigger
- InterruptException
- 字节序大小端转换、模拟htons、htonl、ntohs、ntohl
- Linux下find指令的使用
- 1-004.百度云中的百度人脸识别用java开发人脸识别Demo
- java和js实现aes加密解密
- MySQL (九)
- JavaScript设置cookie、读取cookie、删除cookie详解
- php 模拟返回url上一层目录
- Android 8.0 隐式广播豁免列表
- 如何高效优化Android网络连接以降低电耗