[英]Need to convert javascript code to c#
I have found a javascript library that compresses and decompresses a string. 我找到了一个压缩和解压缩字符串的javascript库。 I'm writing a program in C# that creates a javascript.
我正在用C#编写一个创建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.
在这个脚本中,所有图像都需要转换为base64string,然后进行压缩,以便在脚本执行解压缩功能时,解压缩然后显示图像。
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. 压缩和压缩功能都工作正常但我需要压缩器的确切c#版本,以便javascript解压缩程序可以解压缩它。 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#? 任何人都可以帮我将lzw_encode函数转换为C#?
There is an open-source library called Sharp-LZW that provides LZW encoding and decoding in C#. 有一个名为Sharp-LZW的开源库,它在C#中提供LZW编码和解码。 You can find it here: https://code.google.com/p/sharp-lzw/
您可以在此处找到它: https : //code.google.com/p/sharp-lzw/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.