*对*数字进行编码时,有符号和无符号LEB128之间有区别吗?

问题描述

我了解到LEB128 解码器需要知道编码的数字是带符号的还是无符号的,但是 encoder 似乎在两种方法上都是相同的(尽管Wikipedia使用不同的函数对有符号和无符号数字进行编码。

如果在有符号和无符号LEB128中以相同的方式编码正数(仅范围发生变化),而在有符号LEB128中仅出现负数,则创建一个编码任何整数的函数似乎更明智(使用两者的补码)当参数为负时)。

我实现了一个功能,该功能可以按照我所描述的方式工作,并且似乎可以正常工作。

这不是实现细节(除非我误解了)。任何可以对带符号的LEB128进行编码的函数都将使对带符号的LEB128进行编码的任何函数完全冗余,因此永远没有必要同时创建两者。

我使用了JavaScript,但是实际的实现并不重要。有没有理由使用签名的LEB128编码器未签名的编码器?

const toLEB128 = function * (arg) {

    /* This generator takes any BigInt,LEB128 encodes it,and
    yields the result,one byte at a time (little-endian). */

    const digits = arg.toString(2).length;
    const length = digits + (7 - digits % 7);
    const sevens = new RegExp(".{1,7}","g");
    const number = BigInt.asUintN(length,arg);
    const padded = "000000" + number.toString(2);
    const string = padded.slice(padded.length % 7);
    const eights = string.match(sevens).map(function(string,index) {

        /* This callback takes each string of seven digits and its
        index (big-endian),prepends the correct continuation digit,converts the 8-bit result to a BigInt,then returns it. */

        return BigInt("0b" + Boolean(index) * 1 + string);
    });

    while (eights.length) yield eights.pop();
};

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)