2011-03-10 68 views
1

我想例如轉換:字符串轉換爲在其簡單的形式.NET URI

AION到URI字符串,因此使用System.Uri.EscapeDataString這個翻譯爲艾%C3%B3N但我希望Ai%F3n

如何翻譯字符,我想要的方式?

我使用的是常規的WinForm應用程序不是一個ASP頁面

+0

你能不能引用System.Web.dll程序和使用HtmlUtility.HtmlEncode – 2011-03-10 18:25:08

+0

我已經檢查過,併產生如以下你說同樣的輸出(和HttpUtility不HtmlUtility – Luffy 2011-03-10 18:26:45

+0

這幾乎聽起來像一個字符集問題。 – Powerlord 2011-03-10 18:32:05

回答

1

感謝@保羅McCowat最後asnwer在那個鏈接我想出了一個功能,做我想要的:

public static string ConvertToUri(string uri_string) 
     { 
      StringBuilder Encoded = new StringBuilder(); 
      foreach (char Ch in uri_string) 
      { 
       if (Uri.EscapeUriString(Ch.ToString()) != Ch.ToString()) 
       { 
        Encoded.Append("%"); 
        Encoded.AppendFormat("{0:x2}", Encoding.Unicode.GetBytes(Ch.ToString())[0]); 
       } 
       else 
       { 
        Encoded.Append(Ch); 
       } 
      } 
      return Encoded.ToString(); 
     } 
+1

此代碼不會爲代碼高於256範圍的字符生成正確的結果。確保你對此感到滿意。 – 2011-03-10 21:05:45

3

Server.HTMLEncode

這將使你

"Aión" 
+0

如果它輸出這不是我所需要的...我完全需要Ai%F3n和...在非服務器應用程序中我找不到函數 – Luffy 2011-03-10 18:24:11

相關問題