【问题标题】:Need help identifying and computing a number representation需要帮助识别和计算数字表示
【发布时间】:2010-06-24 01:58:26
【问题描述】:

我需要帮助识别以下数字格式。

例如,MIB中的以下数字格式:

0x94 0x78 = 2680
0x94 0x78 in binary: [1001 0100] [0111 1000]

似乎如果MSB为1,则表示后面有另一个字符。如果是0,就是数字的结尾。

所以值 2680 是 [001 0100] [111 1000],正确格式化是 [0000 1010] [0111 1000]

这种数字格式叫什么,除了位操作和转移到更大的无符号整数之外,还有什么好的计算方法?

【问题讨论】:

    标签: numbers bit-manipulation


    【解决方案1】:

    我见过这称为 7bhm(7-bit has-more)或 VLQ(可变长度数量);见http://en.wikipedia.org/wiki/Variable-length_quantity

    这是存储大端(最高有效字节在前),而不是 Encoding an integer in 7-bit format of C# BinaryReader.ReadString 中描述的 C# BinaryReader.Read7BitEncodedInt 方法

    我不知道除了位操作之外的任何解码方法。

    可以在以下位置找到示例 PHP 代码 http://php.net/manual/en/function.intval.php#62613

    或者在 Python 中我会做类似的事情

    def encode_7bhm(i):
        o = [ chr(i & 0x7f) ]
        i /= 128
    
        while i > 0:
            o.insert(0, chr(0x80 | (i & 0x7f)))
            i /= 128
    
        return ''.join(o)
    
    
    def decode_7bhm(s):
        o = 0
    
        for i in range(len(s)):
            v = ord(s[i])
            o = 128*o + (v & 0x7f)
    
            if v & 0x80 == 0:
                # found end of encoded value
                break
        else:
            # out of string, and end not found - error!
            raise TypeError
    
        return o
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-17
      • 1970-01-01
      • 2021-08-29
      • 1970-01-01
      • 2023-01-29
      • 1970-01-01
      • 2012-10-03
      • 1970-01-01
      相关资源
      最近更新 更多