Click here to Skip to main content
15,895,370 members

LZW Compressor Byte Problem

SSilver009 asked:

Open original thread
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();
Tags: C#

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900