Redis之小端转大端
来源:互联网 发布:java算法技术手册 编辑:程序博客网 时间:2024/05/16 07:30
redis的Endianconv.c文件主要服务于inset集合升级,
其实看以看出它是做了个转换,将小端转成大端的方法
#include <stdint.h>/* Toggle the 16 bit unsigned integer pointed by *p from little endian to * big endian */void memrev16(void *p) { unsigned char *x = p, t; t = x[0]; x[0] = x[1]; x[1] = t;}/* Toggle the 32 bit unsigned integer pointed by *p from little endian to * big endian */void memrev32(void *p) { unsigned char *x = p, t; t = x[0]; x[0] = x[3]; x[3] = t; t = x[1]; x[1] = x[2]; x[2] = t;}/* Toggle the 64 bit unsigned integer pointed by *p from little endian to * big endian */void memrev64(void *p) { unsigned char *x = p, t; t = x[0]; x[0] = x[7]; x[7] = t; t = x[1]; x[1] = x[6]; x[6] = t; t = x[2]; x[2] = x[5]; x[5] = t; t = x[3]; x[3] = x[4]; x[4] = t;}uint16_t intrev16(uint16_t v) { memrev16(&v); return v;}uint32_t intrev32(uint32_t v) { memrev32(&v); return v;}uint64_t intrev64(uint64_t v) { memrev64(&v); return v;}#ifdef TESTMAIN#include <stdio.h>int main(void) { char buf[32]; sprintf(buf,"ciaoroma"); memrev16(buf); printf("%s\n", buf); sprintf(buf,"ciaoroma"); memrev32(buf); printf("%s\n", buf); sprintf(buf,"ciaoroma"); memrev64(buf); printf("%s\n", buf); return 0;}#endif
通过对大小端的存储原理分析可发现,对于 char 型数据,由于其只占一个字节,所以不存在这个问题,这也是一般情况下把数据缓冲区定义成 char 类型 的原因之一。对于 IP 地址、端口号等非 char 型数据,必须在数据发送到网络上之前将其转换成大端模式,在接收到数据之后再将其转换成符合接收端主机的存储模式。
Linux 系统为大小端模式的转换提供了 4 个函数,输入 man byteorder 命令可得函数原型:
#include <arpa/inet.h> uint32_t htonl(uint32_t hostlong); uint16_t htons(uint16_t hostshort); uint32_t ntohl(uint32_t netlong); uint16_t ntohs(uint16_t netshort);
大小端转换方法:
#include<stdlib.h>
#include<stdio.h>
#define Tranverse16(X) ((((UINT16)(X) & 0xff00) >> 8) |(((UINT16)(X) & 0x00ff) << 8))
#define Tranverse32(X) ((((UInt32)(X) & 0xff000000) >> 24) | /
(((UInt32)(X) & 0x00ff0000) >> 8) | /
(((UInt32)(X) & 0x0000ff00) << 8) | /
(((UInt32)(X) & 0x000000ff) << 24))
0 0
- Redis之小端转大端
- unicode编码之大端
- 大端小端之我见
- 存储格式之大端小端详解
- ARM存储格式之 大端小端
- 字节序之大端小端
- C/C++之大端小端
- redis之redis.conf
- Redis之初识Redis
- ARM存储格式之 大端小端(彻底明白大端小端)
- c++ float类型小端转大端存储
- Redis之Redis配置文件redis.conf 详解
- Redis实战之Redis + Jedis
- Redis实战之Redis + Jedis
- Redis 之 Redis的作用
- Redis实战之Redis + Jedis
- Redis : ServiceStack.Redis之IRedisClient
- Redis实战之Redis + Jedis
- caffe layer
- Maven3.0.4基础原理分析
- Introduce to algorithm--------pseudo code to C/C++ code (chapter 16)
- gdb tips 总结
- pyqt5程序使用py2exe打包后运行时报找不到Qt platform plugin ‘windows’错误
- Redis之小端转大端
- 作为软件测试人员,如何描述缺陷(Defect)?
- HDU 1711 Number Sequence
- MySQL数据库游标
- 从本地JSON文件中读取数据
- html学习笔记(8)
- STM32时钟讲解(内部构架说明)
- CoreMotion框架中加速度计使用简明教程
- mysql日志详细解析