带有溢出位的数字的二进制存储......这种格式如何称呼?

发布于 2024-10-16 02:39:45 字数 667 浏览 4 评论 0原文

对于序列化/协议格式,我必须以节省空间的方式将无符号数字编码到无符号 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)

我对此有两个问题:

  1. 这种格式是如何调用的?我在哪里可以查找实现?

  2. 这适用于通过套接字发送的二进制协议,其中 <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:

  1. How is this format called? Where can I look up implementations?

  2. 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 技术交流群。

扫码二维码加入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...

眼睛会笑 2024-10-23 02:39:45

好吧,经过一番研究,我终于找到了。它称为“可变长度数量”,用于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文