[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: [QUIZ] Huffman Encoder (#123

Ball, Donald A Jr (Library)

5/15/2007 9:57:00 PM

> 2007/5/15, Ball, Donald A Jr (Library) <donald.ball@nashville.gov>:
> <snip>
> > I also tried writing a binary encoder which packs and
> unpacks the bit
> > strings generated by my string encoder, but it fails to
> decompress the
> > entire text for long files, e.g. the program source code
> itself. I'm
> > unsure why, perhaps someone more experienced in this arena
> can suggest
> > why.
>
> Perhaps because the file you want to encode already contains
> your end-marker. The decoder stops when it encounters the
> first end-marker (line 161). A binary file would not be
> unlikely to contain a NULL character somewhere.

That was my first thought, but I threw in a check for the existence of
NULL characters in the frequency hash before merging the EOS=>0 hash,
and it did not complain. Besides, the file I'm trying to decompress is
the ruby source of my quiz solution, which is just plain ol' text.

- donald

1 Answer

Raf Coremans

5/15/2007 10:19:00 PM

0

2007/5/15, Ball, Donald A Jr (Library) <donald.ball@nashville.gov>:
> > 2007/5/15, Ball, Donald A Jr (Library) <donald.ball@nashville.gov>:
> > <snip>
> > > I also tried writing a binary encoder which packs and
> > unpacks the bit
> > > strings generated by my string encoder, but it fails to
> > decompress the
> > > entire text for long files, e.g. the program source code
> > itself. I'm
> > > unsure why, perhaps someone more experienced in this arena
> > can suggest
> > > why.
> >
> > Perhaps because the file you want to encode already contains
> > your end-marker. The decoder stops when it encounters the
> > first end-marker (line 161). A binary file would not be
> > unlikely to contain a NULL character somewhere.
>
> That was my first thought, but I threw in a check for the existence of
> NULL characters in the frequency hash before merging the EOS=>0 hash,
> and it did not complain. Besides, the file I'm trying to decompress is
> the ruby source of my quiz solution, which is just plain ol' text.
>
> - donald
>

Strange... I get this:


Encoding-decoding paste-61871.rb:
$ ruby -w paste-61871.rb -b -f paste-61871.rb | ruby -w paste-61871.rb
-b -d > ref
$ cmp paste-61871.rb ref

=>Identical


Encoding-decoding a binary file:
$ ruby -w paste-61871.rb -b -f dilbert2073317070515.gif | ruby -w
paste-61871.rb -b -d > ref
$ cmp dilbert2073317070515.gif ref
cmp: EOF on ref
$ hexdump -C ref
00000000 47 49 46 38 39 61 58 02 d5 |GIF89aX..|
00000009
$ hexdump -C dilbert2073317070515.gif | head -n 1
00000000 47 49 46 38 39 61 58 02 d5 00 c4 00 00 58 58 58 |GIF89aX......XXX|

=> Encoded-decoded terminates just before first NULL character in original file


Regards,
Raf