从基数256转换为多基数和返回的算法

时间:2015-11-03 06:34:03

标签: algorithm data-conversion base-conversion

我有一个字节数据流,也称为基数256符号。什么是最好的算法,将理想情况下即时转换为符号的新流,其中每个符号的基数变化,只在运行时知道?输入字节流和目标基数列表的长度都很长但有限。所有非负整数,没有浮点。此外,目标基数不能保证均匀分割或是256的倍数。

1 个答案:

答案 0 :(得分:1)

您的问题是算术编码的一个子集,它被用作许多压缩算法的最后一个阶段。这是CS中最酷的事情之一:

http://www.drdobbs.com/cpp/data-compression-with-arithmetic-encodin/240169251 https://en.wikipedia.org/wiki/Arithmetic_coding

您的问题具体如何:

您想要的编码器是算术解码器,对于每个解码,您将使用不同大小的字母(基数),所有符号具有相同的概率。

编码器的主循环将执行以下操作:

int val=0; //information from the stream
int range=1; //val is in [0,range)
while(...)
{
    int radix = next_radix();
    //ensure adequate efficiency
    while(range < radix*256)
    {
        val = (val<<8)|(next_byte()&255);
        range<<=8;
    }
    int output = (int)(radix*(long)val/range);
    //find the smallest possible val that produces this output
    int low = (int)((output*(long)range+radix-1)/radix);
    //find the smallest possible val that produces the next output
    int high = (int)(((output+1)*(long)range+radix-1)/radix);
    val-=low;
    range = high-low;
    write(output);
} 

在解码器(算术编码器)中处理终止条件和处理载荷存在复杂性,因此您必须从我链接的内容开始阅读文献。我希望这能让你了解它的工作原理。

祝你好运