2013-04-23 62 views
2

按照W3C標準解碼asp.net中的特殊字符。 ASCII - 網址編碼圖表。一些特殊字符沒有被轉換,而是轉換爲「?」,檢查下面的問題,%92 ASCII的實際結果是「`」,我試圖實現這個解碼爲一個urlencoding相等的字符。在asp.net 4.0中解碼 - 特殊字符

strurl="Workers%92+Accommodation"; 
string strdecode=Server.UrlDecode(strurl); 

例如:ASCII代碼%92(根據W3C standarards URL編碼爲' - 這是不存在在按鍵板參閱http://www.w3schools.com/TAGS/ref_urlencode.asp)。

回答

2

您必須使用適當的編碼代碼頁進行解碼。從http://www.ascii-code.com報價:

There are several different variations of the 8-bit ASCII table. The table below is according to ISO 8859-1, also called ISO Latin-1....

http://en.wikipedia.org/wiki/Windows_code_page,代碼頁ISO Latin-1的是1252。爲了解碼你的字符串,只要做到以下幾點:

strurl = Encoding.GetEncoding(1252). 
      GetString(HttpUtility.UrlDecodeToBytes("%92")); 
+0

它沒有按照HTML URL編碼參考(http://www.w3schools.com/TAGS/ref_urlencode.asp) – Jay 2013-04-23 23:28:47

+0

給出預期的結果謝謝,我來了解First First需要確定頁面編碼,然後解碼考慮相同的字符集..NET默認字符串總是在Unicode編碼,URL解碼發生默認字符集UTF 32 ...糾正我如果我錯了..任何如何給了我想要的結果。 – Jay 2013-04-24 00:05:05

+0

@Janardhan對不起,我不明白你的問題。你能否詳細說明一下? – Kamyar 2013-04-24 12:58:59

4

嘗試編碼/解碼與此:

public static string EncodeString(string decodedString) 
{ 
    return Convert.ToBase64String(Encoding.UTF8.GetBytes(decodedString)); 
} 

public static string DecodeString(string encodedString) 
{ 
    return Encoding.UTF8.GetString(Convert.FromBase64String(encodedString)); 
} 

編輯插件: 當你要通過一個URL中使用的方法來傳輸值是:HttpServerUtility.UrlTokenEncode()方法

編號: http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.urltokenencode.aspx

+0

感謝您的答覆,實際編碼被其他應用程序不在我的控制做了,但是貼到我的網頁的值是「工人%92 +住宿」,需要將其轉換爲純文本,而URLdecode不會發生這種情況。通過上面的函數,decodestring返回「?」。 – Jay 2013-04-23 07:55:20

+0

當您要通過URL傳輸值時,要使用的方法是:HttpServerUtility.UrlTokenEncode()方法 – kobe 2013-04-23 08:07:58

+0

正確,但我不擔心發佈數據,擔心在表單中收到的發佈數據。 – Jay 2013-04-23 08:10:43