CodeProject C#比特流类给出零数组

时间:2015-02-24 18:12:57

标签: c# stream bitstream

我尝试使用Bill Koukoutsis'将一大块数据从8位打包转换为7位打包。来自CodeProject.com的BitStream库似乎仍然是C#中此类事物的首选来源。我使用的代码如下(fs是在代码之外初始化的文件流):

for (int block = 0; block < count; block++)
{
    var theBlock = fetcher(block);
    if (theBlock.Any(x => (x != 0)))
    {
        Console.Write("Good Block =>");
    }
    else
    {
        Console.Write("Zero block =>");
    }
    var sevenBitSize = theBlock.Length - (theBlock.Length/8);
    var theCodedBlock = new byte[sevenBitSize];
    var ms = new MemoryStream(theCodedBlock);
    BitStream bms = ms;
    using (bms)
    {
        foreach (byte t in theBlock)
        {
            bms.Write(t, 0, 7);
        }
        bms.WriteTo(fs);
    }
    if (theCodedBlock.Any(x => (x != 0)))
    {
         Console.Write("Good Block\n");
    }
    else
    {
         Console.Write("Zero block\n");
    }
}

运行时,会产生一个恒定的流&#34; Good Block =&gt;零块&#34;。因此,由于某种原因,BitStream正在输出零到theCodedBlock数组中。它似乎适用于从代码中其他地方的内存数组中读取值,所以我做错了什么或者这是一个BitStream错误?

1 个答案:

答案 0 :(得分:0)

刚发现问题:

BitStream库不能正确支持写入其加载的流;它写入其内部缓冲区但没有做任何事情将其写回流。您必须创建一个空的BitStream,写入,然后将其转换回MemoryStream