日期:2014-05-18  浏览次数:20874 次

C#如何将类似“...ad=e5=9b=bd...”的UFT8码转换为汉字。见例子:
UFT8码是:"=?utf-8?Q?=e4=b8=ad=e5=9b=bd=e7=a7=91=e6=8a=80=e8=ae=ba=e6=96=87=e5=9c?="

对应的汉字是:"中国科技论文在线-首发论文退稿"

C#如何转换?

------解决方案--------------------
//转成utf-8

string str = "中国科技论文在线-首发论文退稿";

byte[] utf8 = Encoding.UTF8.GetBytes(str);
string s3 = "";
foreach (byte b in utf8)
{
s3 += "=" + string.Format("{0:X2}", b);

}
Response.Write(s3 + "<br />");




//转成汉字
string cd = "=E4=B8=AD=E5=9B=BD=E7=A7=91=E6=8A=80=E8=AE=BA=E6=96=87=E5=9C=A8=E7=BA=BF=2D=E9=A6=96=E5=8F=91=E8=AE=BA=E6=96=87=E9=80=80=E7=A8=BF";
string[] b6 = cd.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
byte[] bs = new byte[b6.Length];
int i = 0;
while (i<b6.Length)
{
bs[i] = (byte)Convert.ToByte(b6[i], 16);

i++;
}

Response.Write(Encoding.GetEncoding("UTF-8").GetString(bs));