0

I have found a javascript library that compresses and decompresses a string. I'm writing a program in C# that creates a javascript. in this script, all images needs to be converted to base64string and then compressed so that when the script is executed the decompress function, decompress then and show the images.

both compress and the compress functions work fine but I need the exact c# version of the compressor so that javascript decompressor can decompress it. here is the library:

     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[i];
            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[i] = String.fromCharCode(out[i]);
        }
        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[i].charCodeAt(0);
            if (currCode < 256) {
                phrase = data[i];
            }
            else {
                phrase = dict[currCode] ? dict[currCode] : (oldPhrase + currChar);
            }
            out.push(phrase);
            currChar = phrase.charAt(0);
            dict[code] = oldPhrase + currChar;
            code++;
            oldPhrase = phrase;
        }
        return out.join("");
    }

can anybody help me with conversion of the lzw_encode function to C#?

2
  • 1
    do you have a specific problem? Check out this link if you just want lzw Commented Apr 4, 2013 at 7:17
  • @DmitryLedentsov as I'm doing the compressing with c# and decompress it with javascript. I need to have the exact c# version of the compression function of the javascript code above. Commented Apr 4, 2013 at 7:20

1 Answer 1

2

There is an open-source library called Sharp-LZW that provides LZW encoding and decoding in C#. You can find it here: https://code.google.com/p/sharp-lzw/

Sign up to request clarification or add additional context in comments.

2 Comments

there are lots of libraries for compressions for c#. the important thing for me is that the copressed text via c# can be decompressed with javascript. let me see if this works for me.
But if you correctly implement the LZW algorithm it should not matter if you use C# or JavaScript. Hopefully the library works out for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.