感谢楼上的回复,用了这个方法:byte[] srcarr = Encoding.Default.GetBytes(ustr);
byte[] desarr = Encoding.Convert(Encoding.Default, Encoding.UTF8, srcarr);
string s = Encoding.UTF8.GetString(desarr, 0, desarr.Length);但是我把网页获取到的unicode传入转换时输出的依然是\uxxxx的形式。
很奇怪,我如果直接把这段\uxxxx作为代码里面的常量传入就能正常decode,求高手指点,多谢!

解决方案 »

  1.   

    请问下为什么这段编码没办法转换成中文呢:
    string str="\u65b0\u7586\u548c\u7530\u5e02\u957f\u963f\u8fea\u529b-\u52aa\u5c14\u4e70\u4e70\u63d0\u6d89\u4e25\u91cd\u8fdd\u7eaa\u88ab\u67e5";
      

  2.   

    本来就是中文,不用转啊
    string str = "\u65b0\u7586\u548c\u7530\u5e02\u957f\u963f\u8fea\u529b-\u52aa\u5c14\u4e70\u4e70\u63d0\u6d89\u4e25\u91cd\u8fdd\u7eaa\u88ab\u67e5";
                Console.WriteLine(str);
      

  3.   

     string value = "\u65b0\u7586\u548c\u7530\u5e02\u957f\u963f\u8fea\u529b-\u52aa\u5c14\u4e70\u4e70\u63d0\u6d89\u4e25\u91cd\u8fdd\u7eaa\u88ab\u67e5";
                Console.WriteLine(Uri.UnescapeDataString(value));//输出:新疆和田市长阿迪力-努尔买买提涉严重违纪被查 
      

  4.   

    下面这段代码是自动获取新浪新闻的,但是在处理新闻标题的时候有部分unicode编码就无法输出,求高人指点,多谢。using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Net;
    using System.IO;
    using System.Text.RegularExpressions;namespace ConsoleApplication1
    {
        class Program
        {
           static public string GetWebClient(string url)
            {
                string strHTML = "";
                WebClient myWebClient = new WebClient();
                Stream myStream = myWebClient.OpenRead(url);
                StreamReader sr = new StreamReader(myStream, System.Text.Encoding.GetEncoding("utf-8"));
                strHTML = sr.ReadToEnd();
                myStream.Close();
                return strHTML;
            }       static public string DeUnicode(string str)
            {
                string outStr = "";
                if (!string.IsNullOrEmpty(str))
                {
                    string[] strlist = str.Replace("\\", "").Split('u');
                    try
                    {
                        for (int i = 1; i < strlist.Length; i++)
                        {
                            //将unicode字符转为10进制整数,然后转为char中文字符
                            outStr += (char)int.Parse(strlist[i], System.Globalization.NumberStyles.HexNumber);
                        }
                    }
                    catch (FormatException ex)
                    {
                        outStr = ex.Message;
                    }
                }
                return outStr;
            }        static void Main(string[] args)
            {
                DateTime Date = DateTime.Now;
                string url = "http://top.news.sina.com.cn/ws/GetTopDataList.php?top_type=day&top_cat=www_all&top_time=" + Date.Year.ToString() + Date.Month.ToString().PadLeft(2, '0') + Date.Day.ToString().PadLeft(2, '0') + "&top_show_num=100&top_order=ASC&js_var=all_1_data";
                string html = GetWebClient(url);
    //            Clipboard.SetDataObject(html, true);
                Match mc = Regex.Match(html, "\"title\":\"([^\"]+)\"[^\\}]+?\"url\":\"([^\"]+)\"[^\\}]+?\"create_date\":\"\\d{4}-(\\d+-\\d+)\"[^\\}]+?\"create_time\":\"(\\d+:\\d+)");            string title = "";
                while (mc.Success)
                {
                    title = DeUnicode(mc.Groups[1].Value);
                    Console.WriteLine(title + " " + mc.Groups[3].Value);
                    mc = mc.NextMatch();
                }
    Console.ReadLine();
            }
        }
    }
      

  5.   

    把你的DeUnicode替换一下就可以了,我试了没报错
    m=>代码的意思就是找到所有的\uxxxx,转换为short类型,如果不能转就原样输出static public string DeUnicode(string s)
            {
                Regex reUnicode = new Regex(@"\\u([0-9a-fA-F]{4})", RegexOptions.Compiled);
                return reUnicode.Replace(s, m =>
                {
                    short c;
                    if (short.TryParse(m.Groups[1].Value, System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture, out c))
                    {
                        return "" + (char)c;
                    }
                    return m.Value;
                });
    }
      

  6.   

    感谢楼上,我这边编译后还是报错呢,信息如下:1>------ 已启动生成: 项目: ConsoleApplication1, 配置: Debug Any CPU ------
    1>D:\vcwork\ConsoleApplication1\ConsoleApplication1\Program.cs(28,42,28,43): error CS1660: 无法将 lambda 表达式 转换为类型“string”,原因是它不是委托类型
    1>D:\vcwork\ConsoleApplication1\ConsoleApplication1\Program.cs(31,101,31,112): error CS0103: 当前上下文中不存在名称“CultureInfo”
    ========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========谢谢回答