2013-03-10 67 views
0

在開發Windows Phone的過程中,我想編寫一個utf8編碼的URI,但代碼失敗。結果仍然是unicode但不是%D6%DC的形式...如何在WP中將Unicode轉換爲utf8

 byte[] unicodeBytes = Encoding.Unicode.GetBytes(str); 
     byte[] utf8bytes = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, unicodeBytes); 
     char[] utf8Chars = new char[Encoding.UTF8.GetCharCount(utf8bytes, 0, utf8bytes.Length)]; 
     Encoding.UTF8.GetChars(utf8bytes, 0, utf8bytes.Length, utf8Chars, 0); 
     string utf8string = new string(utf8Chars); 
     var URI = new Uri("http://www.jpwy.net/gc/search.php?" + utf8string); 

這是怎麼回事?

回答

2

我不確定你的目的是什麼,但是有什麼理由不使用HttpUtility.UrlEncode來編碼字符串嗎? Urlencode的默認編碼是UTF8。

你應該只能夠做

var URI = new Uri("http://www.jpwy.net/gc/search.php?" + HttpUtility.UrlEncode(str)); 
+0

由於更換上面的代碼。這很方便,很好。我使用的是uri.toString,所以它在輸出中轉換回Unicode。我會採取這個解決方案。 – thsieh 2013-03-10 16:26:26