@下面是asp.net中的編碼轉換方法:
中文轉Unicode:HttpUtility.UrlEncodeUnicode(string str);
轉換后中文格式:"%uxxxx" 舉例:"柳_abc123" 轉換結果是:"%u67f3_abc123"
Unicode轉中文1:HttpUtility.UrlDecode(string str);
str格式:"%uxxxx" ,舉例:"%u67f3_abc123"
Unicode轉中文2:Regex.Unescape(string str);
str格式:"\uxxxx" ,舉例:"\u67f3_abc123"
@下面是Javascript中的編碼轉換方法:
1.window.escape()與HttpUtility.UrlEncodeUnicode()編碼格式一樣:將一個漢字編碼為%uxxxx格式(unicode編碼)
不會被window.escape編碼的字符有:@ _ - . * / + 這與http://www.w3school.com.cn/js/jsref_escape.asp上的解釋不符合
2.window.encodeURIComponent()與HttpUtility.UrlEncode()編碼格式一樣:將一個漢字編碼為%xx%xx%xx的格式(gbk編碼或者utf8編碼)
不會被window.encodeURIComponent編碼的字符有:'()*-._!~ 這與http://www.w3school.com.cn/js/jsref_encodeURIComponent.asp解釋相符合
不會被HttpUtility.UrlEncode編碼的字符有:'()*-._!相比較而言,HttpUtility.UrlEncode比window.encodeURIComponent多一個 ~ 編碼
3.不會被window.encodeURI編碼的字符有:-_.!*();/?:@&=$,# 與encodeURIComponent對比,發現encodeURI不對:;/?:@&=+$,#這些用於分隔 URI 組件的標點符號進行編碼
@Asp.Net編碼與JS編碼的區別:
1. 不會被HttpUtility.UrlEncodeUnicode編碼的字符與不會被HttpUtility.UrlEncode編碼的字符一樣,而escape和encodeURIComponent不編碼的字符不一樣
2. HttpUtility.UrlEncode和HttpUtility.UrlEncodeUnicode會對/編碼,而escape和encodeURIComponent會對/編碼,encodeURI不會對/編碼
3. HttpUtility.UrlEncode()和HttpUtility.UrlEncodeUnicode()會把空格編碼為 +,而escape,encodeURIComponent,encodeURI都會將空格編碼為%20
@為什么優先使用encodeURIComponent而不是escape?
escape方法並不編碼字符+。而我們知道,在用戶提交的表單字段中,如果有空格,則會被轉化為+字符,而服務器解析的時候則會認為+號代表空格。由於這個缺陷,escape方法並不能正確地處理所有的非ASCII字符,你應當盡量避免使用escape方法,取而代之,你最好選擇 encodeURIComponent()方法。