Click here to Skip to main content
15,896,063 members

LZW Compressor Byte Problem

Revision 3
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

C#
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 = 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...

C#
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();
Posted 12-Nov-12 15:18pm by SSilver009.
Tags: