我發現了一個壓縮和解壓縮字符串的JavaScript庫。我正在用C#編寫一個程序來創建一個javascript。在這個腳本中,所有圖像都需要轉換爲base64string,然後進行壓縮,以便在腳本執行解壓縮功能時,解壓縮並顯示圖像。需要將JavaScript代碼轉換爲C#
這兩個壓縮和壓縮功能工作正常,但我需要壓縮機的確切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#嗎?
你有什麼具體問題嗎?看看[這個鏈接](https://code.google.com/p/sharp-lzw/source/browse/trunk/SharpLZW/LZW/?r=2)如果你只是想要lzw – 2013-04-04 07:17:14
@DmitryLedentsov,因爲我是用c#進行壓縮並用javascript解壓縮。我需要上面的JavaScript代碼的壓縮功能的確切c#版本。 – 2013-04-04 07:20:01