Protobuf中的变长编码:Varints
来源:互联网 发布:阿里云看我的订单 编辑:程序博客网 时间:2024/05/22 04:58
protobuf 中对int32, int64,uint32,uint64, sint32,sint64,bool,enum的变量采用变长编码来存储。
变长编码用1个或更多个字节来表示整数,对小数值采用更少的字节。下面先举几个例子:
1: 00000001
300: 10101100 00000010
在变长编码中每个字节的最高位被当做最高有效位(msb),如果为1表明这个整数还没有完结。
我们来看一下如果由 10101100 00000010 得到300:
10101100 -> msb 为1,第七位为0101100,继续读入下一个字节
00000010 -。msb为1,此整数结束
0000010 ++ 0101100
-> 000000001 00101100
-> 256 + 32 + 8 + 4 = 300
让我们看一下 编码过程(leveldb util/coding.cc) 非常简单
char* EncodeVarint64(char* dst, uint64_t v) {
static const int B = 128; // 1000 0000
unsigned char* ptr = reinterpret_cast<unsigned char*>(dst);
while (v >= B) {
*(ptr++) = (v & (B-1)) | B; //取低七位
v >>= 7;
}
*(ptr++) = static_cast<unsigned char>(v);
return reinterpret_cast<char*>(ptr);
}
- Protobuf中的变长编码:Varints
- protobuf【2】Varints方法编码
- protobuf中的Base 128 Varints类型分析
- protobuf中的Base 128 Varints类型分析
- Base 128 Varints 编码
- ProtoBuf中的编码
- ProtoBuf中的编码
- google的Base 128 Varints编码
- 变长编码
- UTF8编码-变长编码
- 深入浅出Google-ProtoBuf中的编码规则
- 深入浅出Google-ProtoBuf中的编码规则
- protobuf中的variable-integer编码方式
- protobuf编码
- 使用自己的Python函数处理Protobuf中的字符串编码
- 宏中的变长参数
- python中的变长参数
- C 中的变长数组
- hdu1312 Red and Black(简单搜索)
- 非成员函数、成员函数区别
- Ruby on Rails 学习(四)
- Java内存溢出详解
- 火狐浏览器为何能成为智能手机操作系统?
- Protobuf中的变长编码:Varints
- 【js学习笔记-045】-- 类对象数组
- 多校第一场——hdu4604——dp
- 【js学习笔记-046】-- 函数
- android音乐文件乱码显示问题解决
- Cannot find message resources under key org.apache.struts.action.MESSAGE
- unity3d for Android实现欢迎引导页面的功能
- JAVA 多线程
- SQLite