Click here to Skip to main content

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

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.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).



Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 26 Mar 2009
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100