How to use C # to encode and decode "Chinese" characters

In my ASP.NET MVC application I am using the Chinese category name, it appears as %E8%82%B2%E5%84%BF

in the IE url, but the actual value is "θ‚²ε„Ώ".

I want to know how can I convert 'θ‚²ε„Ώ' to %E8%82%B2%E5%84%BF

in C # and how can I convert it back. Is it possible to display "θ‚²ε„Ώ" directly in a URL link? Will this be good for SEO?

+2


source to share


3 answers


The text displayed in the IE address bar is the URL encoded form of the hexadecimal version of these characters. The hexadecimal version of 'θ‚²ε„Ώ' encoded in UTF-8 is E882B2E584BF:

byte[] buffer = new byte[] { 0xE8, 0x82, 0xB2, 0xE5, 0x84, 0xBF };
string s = Encoding.UTF8.GetString(buffer);

      



s equals 'θ‚²ε„Ώ'.

You must not pass direct Chinese characters in the url, it must be url encoded using HttpServerUtility.UrlEncode and UrlDecode .

+6


source


HttpUtility.UrlEncode will encode the url and HttpUtility.UrlDecode will change it.

Example:



string orig = "http://example.com/θ‚²ε„Ώ";
string encoded = HttpUtility.UrlEncode(orig);
// encoded should equal "http://example.com/%E8%82%B2%E5%84%BF"

      

+1


source


Have you checked if you are using Unicode encoding (instead of Default)? The default encoding will not handle Chinese characters.

0


source