# \[AS1\] LZW compression

**URL:** <https://forum.kirupa.com/t/as1-lzw-compression/314182>\
**Category:** flash\
**Created:** [September 21, 2010, 6:10am UTC](https://forum.kirupa.com/t/as1-lzw-compression/314182 "2010-09-21T06:10:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![nitinmukesh](https://avatars.discourse-cdn.com/v4/letter/n/7feea3/32.png) [@nitinmukesh](https://forum.kirupa.com/u/nitinmukesh)\
**Post date:** [September 21, 2010, 6:10am UTC](https://forum.kirupa.com/t/as1-lzw-compression/314182/1 "2010-09-21T06:10:00Z")

</div>

Hi,

I am trying to use a javascript function in Actionscript 1. It is used to compress/decompress the string using LZW algorithm. The compression part works fine but decompression is not working.

```php
// LZW-compress a string
function lzw_encode(s) {
    var dict = {};
    var data = (s + "").split("");
    var out = [];
    var currChar;
    var phrase = data[0];
    var code = 256;
    for (var i=1; i<data.length; i++) {
        currChar=data*;
        if (dict[phrase + currChar] != null) {
            phrase += currChar;
        }
        else {
            out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0));
            dict[phrase + currChar] = code;
            code++;
            phrase=currChar;
        }
    }
    out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0));
    for (var i=0; i<out.length; i++) {
        out* = String.fromCharCode(out*);
    }
    return out.join("");
}

// Decompress an LZW-encoded string
function lzw_decode(s) {
    var dict = {};
    var data = (s + "").split("");
    var currChar = data[0];
    var oldPhrase = currChar;
    var out = [currChar];
    var code = 256;
    var phrase;
    for (var i=1; i<data.length; i++) {
        var currCode = data*.charCodeAt(0);
        if (currCode < 256) {
            phrase = data*;
        }
        else {
           phrase = dict[currCode] ? dict[currCode] : (oldPhrase + currChar);
        }
        out.push(phrase);
        currChar = phrase.charAt(0);
        dict

```

= oldPhrase + currChar;  
code++;  
oldPhrase = phrase;  
}  
return out.join("");  
}

var compressed = lzw\_encode(“Nitin Mukesh Nitin Mukesh Nitin Mukesh Nitin Mukesh Nitin Mukesh Nitin Mukesh Nitin Mukesh Nitin Mukesh Nitin Mukesh Nitin Mukesh”);  
trace(compressed);

trace(lzw\_decode(compressed));

```auto
Output

```

Compressed : Nitin Mukesh ĀĂĄĆĈĊČāăąćĉċčĕĐĘēĎĖđęĔďėĒĚĤġĞěĥĢğĜĦģĠĘ  
Decompressed : Nitin Mukesh

```auto

Could not figure out what is the problem.
```
