如何使用C#编码和解码'中文'字符 [英] How to use C# encode and decode 'Chinese' characters

查看:203
本文介绍了如何使用C#编码和解码'中文'字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP.NET MVC应用程序中,我使用中文类别名称,它在IE的URL地址中显示为%E8%82%B2%E5%84%BF 但实际价值是育儿。



我想知道如何将育儿转换成%E8%82%B2%E5%84%BF 在C#中,如何将它转换回来。是否可以直接在URL链接中显示育儿?是否对SEO有好处?

解决方案

IE地址栏中显示的文字是这些十六进制版本的URL编码格式字符。以UTF-8编码的育儿十六进制版本为E882B2E584BF:

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

s等于'育儿'。



您不应该在URL中传递直接的汉字,它应该使用 HttpServerUtility.UrlEncode UrlDecode


In my ASP.NET MVC application i am using Chinese Category Name, it was displayed as %E8%82%B2%E5%84%BF in IE's URL address, but the actual value is '育儿'.

I want to know how can I convert '育儿' into %E8%82%B2%E5%84%BF in C# and how can I convert it back, too. Is it possible display '育儿' in the URL link directly? Will it be good for SEO?

解决方案

The text displayed in IE's address bar is the URL encoded form of the hex version of those characters. The hex 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 is equal to '育儿'.

You shouldn't transmit the straight chinese characters in the URL, it should be URL encoded using HttpServerUtility.UrlEncode and UrlDecode.

这篇关于如何使用C#编码和解码'中文'字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆