我找到了一个压缩和解压缩字符串的javascript库。我正在用C#编写一个创建javascript的程序。在这个脚本中,所有图像都需要转换为base64string,然后进行压缩,以便在脚本执行解压缩功能时,解压缩然后显示图像。
compress和compress函数都运行正常,但我需要压缩器的c#版本,以便javascript解压缩程序可以解压缩它。这是图书馆:
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("");
}
任何人都可以帮我将lzw_encode函数转换为C#?
答案 0 :(得分:2)
有一个名为Sharp-LZW的开源库,它在C#中提供LZW编码和解码。您可以在此处找到它:https://code.google.com/p/sharp-lzw/