thsieh
thsieh

Reputation: 620

How to convert unicode to utf8 in WP

With Windows Phone development, I would like to compose an URI with utf8 encoded with the following code but failed. The result is still unicode but not the form of %D6%DC...

        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);

What's wrong with this?

Upvotes: 0

Views: 565

Answers (1)

David Gordon
David Gordon

Reputation: 554

I'm not sure what your purpose is, but is there any reason not to use HttpUtility.UrlEncode to encode the string? The default encoding for Urlencode is UTF8.

You should just be able to do replace the above code with

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

Upvotes: 2

Related Questions