2015-09-12 71 views
-1

C#中JavaScript函數的等效函數是什麼?
這裏是我試圖轉換爲C#的JavaScript語句:C# - UTF8⇔二進制,十六進制和Base 64轉換器

utf82rstr = function (input) { 
    var output = "", i = 0, c = c1 = c2 = 0; 

    while (i < input.length) { 
     c = input.charCodeAt(i); 

     if (c < 128) { 
      output += String.fromCharCode(c); 
      i++; 
     } else if ((c > 191) && (c < 224)) { 
      c2 = input.charCodeAt(i + 1); 
      output += String.fromCharCode(((c & 31) << 6) | (c2 & 63)); 
      i += 2; 
     } else { 
      c2 = input.charCodeAt(i + 1); 
      c3 = input.charCodeAt(i + 2); 
      output += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); 
      i += 3; 
     } 
    } 

    return output; 
} 

回答

0

我使用這個功能,爲我工作。我的解決方案:

static string utf82rstr(string input) { 
 
    var output = ""; 
 
    int i = 0, c = 0, c2 = 0, c3 = 0; 
 

 
    while (i < input.Length) { 
 
    var tmp = input[i]; // /n == 10 
 
    c = ((int) input[i]); 
 

 

 
    if (c == 10) { 
 
     output += "~"; 
 
     i++; 
 
    } else if (c == 0) { 
 
     output += ""; 
 
     i++; 
 
    } else if (c < 128) { 
 
     output += Char.ConvertFromUtf32(c); 
 
     i++; 
 
    } else if ((c > 191) && (c < 224)) { 
 
     c2 = ((int) input[i + 1]); 
 
     output += Char.ConvertFromUtf32(((c & 31) << 6) | (c2 & 63)); 
 
     i += 2; 
 
    } else { 
 
     try { 
 
     c2 = (int) input[i + 1]; 
 
     c3 = (int) input[i + 2]; 
 
     output += Char.ConvertFromUtf32(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); 
 
     i += 3; 
 
     } catch (Exception e) { 
 
     output += ""; 
 
     i += 3; 
 
     } 
 
    } 
 
    } 
 
    return output; 
 
}

+1

@RezaAghaei謝謝,但我編輯我的問題。所以,你的回答是不夠的。 –

2

您可以使用Char.ConvertFromUtf32()Char.Parse

//Both of rsult1 and result2 will be "A" 
var result1 = Char.ConvertFromUtf32(int.Parse("0041", System.Globalization.NumberStyles.HexNumber)); 
var result2 = Char.Parse("\u0041").ToString(); 

這裏是你的問題中提到的樣品:

// returns س 
var result = Char.ConvertFromUtf32(((216 & 31) << 6) | (179 & 63)); 
相關問題