Solved

# LZW - again

Posted on 2003-03-25

This is do with LZW.

I am trying to read in codewords as dictionary entries from a file, I read in 7 bits to start with, because 0 - 127

is the number of original entries in my dictionary ( i.e all the ascii values from 0 - 127 ), but when reading bits

from a file, i read each bit at a time but each bit ( up to 8 ) is stored in one byte, however if a number such as

one greater than 255 it will be 9 bits long, so that i need to be able to recognise this. Note that when I compress

I have a current length variable that changes if the binary string that i am about to compress is greater than

its current length, if so i change current length to the size of that string, so that any binary strings (numbers)

processed after the change that are shorter in length than the current length are padded with 0's at the begining i.e

take the following

1001001 //curlength variable is 7

1000101

10010101 //curlength variable is now changed to 8 as string length has changed

1111 //but now we have a binary string of 4 length

will become

1001001 //curlength variable is 7

1000101

10010101 //curlength variable is now changed to 8 as string length has changed

00001111 //curlength is no longer 7 so pad four 0's to make it the length of curlength variable

So when decompressing I will have some codewords padded with 0.

if i reach the number 127 at any point then i change the value of the curlength to 8, if

i reach the value of 255 at any time, then i change the value of the curlength 255 and so on, so that

even if get a code word that would be less than that then i know that it will be padded

with 0s at the begining. The problem is is how do i know when to change as I am reading in 8 bits at a time

so that if a certain number is 9 bits what do I do?

I can post code if that will help, any help greatly appreciated.