在 Protocol Buffer 的编码器规范中https://developers.google.com/protocol-buffers/docs/encoding ,据说:
If you use int32 or int64 as the type for a negative number, the resulting varint is always ten bytes long – it is, effectively, treated like a very large unsigned integer
好吧,看起来不错,但是使用 10 字节长的 varint,您可以存储 70 位长的整数,并且类型是 64 位。因此,有 6 位未使用...这些位应该设置为 1 还是 0?
总结一下,对于int32/64,应该将-1
编码为
FF FF FF FF FF FF FF FF FF 7F
或
FF FF FF FF FF FF FF FF FF 01
?
最佳答案
第 64 位及以上位应为零。因此,在 C++ 中,您可以通过将 int64 转换为 uint64 来对其进行编码,然后像任何其他无符号整数一样对其进行编码(这实际上是 Protobuf C++ 库所做的)。请记住,int32
需要符号扩展为 64 位。
关于protocol-buffers - Protocol buffers 负 int32/int64 编码器的规范是模糊的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28533167/