带有溢出位的数字的二进制存储......这种格式如何称呼?
对于序列化/协议格式,我必须以节省空间的方式将无符号数字编码到无符号 64 位整数,但这种方式仍然很容易实现(这意味着,我不是在寻找专用的压缩算法)。我在想以下问题:
if n<128
take bits 0..6 for representing n, set overflow bit 7 to 0
store one byte
if n>=128 and n<16384
take bits 0..6 of byte 1 as bits 0..6 of n, set overflow bit 7 of byte 1 to 1
take bits 0..6 of byte 2 as bits 7..13 of n, set overflow bit 7 of byte 2 to 0
store byte 1 followed by byte 2
if n>=16384 and n<2^21
...set overflow bit 7 of byte 2 to 1... (and so on)
我对此有两个问题:
这种格式是如何调用的?我在哪里可以查找实现?
这适用于通过套接字发送的二进制协议,其中 <128 的小数字将经常发送。您认为额外的处理值得吗?
For a serialization/protocol format I have to encode unsigned numbers up to unsigned 64bit integer in a space-saving way that should still be easy to implement (meaning, I'm not looking for a dedicated compression algorithm). I was thinking about the following:
if n<128
take bits 0..6 for representing n, set overflow bit 7 to 0
store one byte
if n>=128 and n<16384
take bits 0..6 of byte 1 as bits 0..6 of n, set overflow bit 7 of byte 1 to 1
take bits 0..6 of byte 2 as bits 7..13 of n, set overflow bit 7 of byte 2 to 0
store byte 1 followed by byte 2
if n>=16384 and n<2^21
...set overflow bit 7 of byte 2 to 1... (and so on)
I have two questions about this:
How is this format called? Where can I look up implementations?
This is for a binary protocol that will be sent over sockets, where small numbers <128 will be sent very often. Do you think the extra processing is worth it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与 UTF-8 不一样,但类似。
编辑
顺便说一句:尝试选择一个已知的协议。 UTF-8、霍夫曼编码...
Not the same as, but similar to UTF-8.
Edit
BTW: try and choose a known protocol. UTF-8, Huffman encoding...
好吧,经过一番研究,我终于找到了。它称为“可变长度数量”,用于 MIDI 和 ASN.1(请参阅 维基百科条目)
为了回答我的其他问题,我倾向于认为它不值得处理开销,但我仍在思考它。
Okay, after some more research I've finally found it. It's called 'variable-length quantity' and used in MIDI and ASN.1 (see Wikipedia Entry)
To answer my other question, I'm tending to believe it isn't worth the processing overhead but I'm still pondering about it.