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?
source to share
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 .
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"
source to share