LZW压缩器字节问题 [英] LZW Compressor Byte Problem

查看:82
本文介绍了LZW压缩器字节问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿家伙!

我一直在用C#在LZW压缩机中工作,但遇到了一些问题...

因此,我用255个已知代码构建了词典的开头,然后我开始进行星标编码...问题是,例如,当我尝试将int 256编码为一个字节时,这会产生问题:有人可以帮助我吗?

预先感谢并感谢

这部分是建立字典并压缩文件

Hey guy''s!

I''ve been working in a LZW compressor in c# and i''ve got some problems...

So i build the dictionary initialy with the 255 known codes and then i star coding... the problem is for example when i try to code the int 256 to a byte this gives problems :S can someone help me ?

Cumps and thanks in advance

This part is to build the dictionary and compress a file

while (br.BaseStream.Position < br.BaseStream.Length)
  {

      Console.WriteLine(omg);
      omg++;

      t = br.ReadByte();
      chr = t;
      int aux=-1;

      byte[] res = new byte[str.Count() + 1];

      for (int i = 0; i < str.Count(); i++)
      {
          res[i] = str[i];
      }

      res[str.Count()] = chr;

      int pos = isEqual(res, lista);

      if (pos != -1)
      {
          str = new byte[res.Count()];
          for(int k=0;k<res.Count();k++)
          {
              str[k] = res[k];
          }

      }
      else if (pos==-1)
      {

          aux = isEqual(str, lista);

          byte uh = (byte)aux;
          _FileStream.WriteByte(uh);

          Node nv = new Node();
          nv.by = new Byte[res.Count()];

          for (int k = 0; k < res.Count(); k++)
          {
              nv.by[k] = res[k];
          }

          lista.Add(nv);

          str = new byte[1];
          str[0] = chr;

      }

  }



Lista =字典;
isEqual Function =返回在字典中搜索的字节序列位置的函数


这部分是要解压缩->这就是我读取字节时的问题所在...我不明白我写的是什么...



Lista = Dictionary;
isEqual Function = function that returns the position of sequence of bytes that we are searching in the dictionary


This part was to uncompress -> and this where''s the problem when i read the bytes... i dont get what i have written...

while (br.BaseStream.Position < br.BaseStream.Length)
           {
               t = br.ReadByte();
               if (cnt == 0)
               {
                   NCODE = new byte[1];
                   NCODE[0] = t;
               }
               else
               {
                   NCODE = new byte[NCODE.Count()];
                   NCODE[NCODE.Count()] = t;
               }

               pcr = isEqual(NCODE, lista);

               if (pcr == -1)
               {
                   pcr = isEqual(OCODE, lista);
                   str = new byte[OCODE.Count()];

                   for (int i = 0; i < OCODE.Count(); i++)
                   {
                       str[i] = OCODE[i];
                   }

                   if (cnt > 0)
                   {
                       str = new byte[OCODE.Count() + 1];
                       str[OCODE.Count()] = chr;
                   }
               }
               else if (pcr > -1)
               {
                   pcr = isEqual(NCODE, lista);
                   str = new byte[OCODE.Count()];

                   for (int i = 0; i < OCODE.Count(); i++)
                   {
                       str[i] = OCODE[i];
                   }
               }

               for (int i = 0; i < str.Count(); i++)
               {
                   _FileStream.WriteByte((byte)str[i]);
               }

               chr = str[0];

               Node nv = new Node();
               nv.by = new byte[OCODE.Count() + 1];

               for (int i = 0; i < OCODE.Count(); i++)
               {
                   nv.by[i] = OCODE[i];
               }

               nv.by[OCODE.Count()] = chr;

               OCODE = new byte[NCODE.Count()];

               for (int i = 0; i < NCODE.Count(); i++)
               {
                   OCODE[i] = NCODE[i];
               }

           }

           _FileStream.Close();

推荐答案

如果您需要编码超过256个不同的值(即0-255),则需要使用8个以上的位.在任何意味着从一个字节到两个字节的正常情况下,您都会得到一个16位的 ushort (0-65535).
If you need to encode more than 256 different values, i.e. 0-255, you need to use more than 8 bits. In any normal scenario that means going from one byte to two, giving you a 16 bit ushort (0-65535).


在使用任何技术进行压缩之前,首先必须正确序列化您的数据.这意味着以可逆的方式将您拥有的内容转换为一系列单独的字节.

intInt32的别名.该名称暗示其32位内存消耗.因此,您必须将每个int分成四个字节.

以类似方式处理所有其他类型.请始终记住,此后(解压缩后),您必须从该字节序列中恢复数据.然后,您可以逐字节压缩.
Before compressing with whatever technique, you first have to properly serialize your data. That means to transform what you have in a reversable way into a series of individual bytes.

An int is an alias for an Int32. That name hints to its 32 bit of memory consumption. You therefore have to break every int up into four bytes.

Handle all other types in a similar way. Always keep in mind that you have to restore your data from that byte sequence afterwards (after decompression). Then, you can compress byte by byte.


这篇关于LZW压缩器字节问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆