c# 将gb2312编码转换为汉字

如题
2025-05-21 18:07:29
推荐回答(4个)
回答(1):

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Net;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "中国语言文学系";
            // 按分号分割
            string[] words = str.Split(';');
            string result = string.Empty;
            for (int i = 0; i < words.Length; i++)
            {
                if (!string.IsNullOrEmpty(words[i]))
                {
                    // 转换前,去除每个单词前 &#
                    result += (char)Convert.ToInt32((words[i].Remove(0, 2)));
                }
            }
            // 输出
            Console.WriteLine(result);
        }
    }}

回答(2):

			string str ="中国";
System.Text.RegularExpressions.Regex reg
=new System.Text.RegularExpressions.Regex("&#(\\d+);");
System.Text.StringBuilder builder=new StringBuilder();
foreach (System.Text.RegularExpressions.Match element in reg.Matches(str)) {
builder.Append((char)int.Parse(element.Groups[1].Value));
}
string str1=builder.ToString();
Debug.WriteLine(str1);

回答(3):

string s = "中国语言文学系";
var doc = new System.Xml.XmlDocument();
doc.LoadXml("");
var node = doc.DocumentElement as System.Xml.XmlNode;
node.InnerXml = s;
string ss = doc.DocumentElement.InnerText;

回答(4):

如果是在web 环境,直接用 Server.HtmlDecode 就可以了。。。

如果不是在 web 环境,调用 HttpUtility.UrlDecode() ,在 System.Web 命名空间下。
如果没有,就引用一下 System.Web 程序集