我的一个文档中包含如“礛祃瑿瑿璷璫璵”之类的乱码,我用UltraEdit的Unicode转UTF-8可以将这些乱码准确的转换成正确的字符“µMµl-­C-­C-­o-­c­-m”,至少是显示是正确的,用记事本打开也显示正确。
但是我用C#进行unicode转换成UTF-8时就会出现问题
我的代码如下:
 class Program
    {
        static void Main(string[] args)
        {
            String filename = @"d:\test\encondingtest.txt";
            String newfilename = @"d:\test\encondingtest2.txt";
            String strLin = "";
            try
            {
                StreamReader srfile = new StreamReader(filename, Encoding.Unicode);
                StreamWriter swfile = new StreamWriter(newfilename, false, Encoding.UTF8);
                while ((strLin = srfile.ReadLine()) != null)
                {
                    byte[] mybyte = Encoding.Unicode.GetBytes(strLin);
                    String strOutLine = Encoding.UTF8.GetString(mybyte);
                    swfile.WriteLine(strOutLine);
                    Console.WriteLine(strOutLine);
                }
                srfile.Close();
                swfile.Close();
            }
            catch (IOException e)
            {
                Console.WriteLine(e);
            }
            Console.ReadKey();
        }
    }
文件显示“�M�l�C�C�o�c�m”
请大家帮忙看看是怎么回事,谢谢!

解决方案 »

  1.   

    String filename = @"c:\temp\temp.txt";
    String newfilename = @"c:\temp\temp1.txt";
    String strLin = ""; // 这里已经指定文件的编码就用再多此一举了--Encoding.UTF8.GetString()
    StreamReader srfile = new StreamReader(filename, Encoding.Unicode);
    StreamWriter swfile = new StreamWriter(newfilename, false, Encoding.UTF8); 
    while ((strLin = srfile.ReadLine()) != null)
    {
        swfile.WriteLine(strLin);
    }
    srfile.Close();
    swfile.Close(); 
      

  2.   

                                    while   ((strLin   =   srfile.ReadLine())   !=   null) 
                                    { 
                                            byte[]   mybyte   =   Encoding.Unicode.GetBytes(strLin); 
                                            String   strOutLine   =   Encoding.UTF8.GetString(mybyte); 
                                            swfile.WriteLine(strOutLine); 
                                            Console.WriteLine(strOutLine); 
                                    } 
    ------------------------
    改成:
    string s = srfile.ReadToEnd();
    swfile.write(s);
    就OK
      

  3.   

    to zswang:
    显示还是不对啊?显示的是“䶵沵䎭䎭澭掭涭”
      

  4.   

    首先你的确认d:\test\encondingtest.txt文件是否为Unicode编码,可以用UE打开看看
    不知道你所说的显示是怎么个显示?2楼的方法也是可以的我的测试步骤是,用记事本编辑一些内容然后另存为Unicode文本文件
    然后执行代码,在用记事本打开,格式是Utf8,一切正常。
      

  5.   

    to zswang
    我完全按照你的方式进行操作,输入的内容是“礛祃瑿瑿璷璫璵”,打开输出文件后里面现实的内容仍然是“礛祃瑿瑿璷璫璵”而不是我想要得到的“µMµl-­C-­C-­o-­c­-m”,不知道你是怎样得到的,麻烦你再说一下,谢谢!
      

  6.   

    研究了一下,对于新建的文档UE里先转成us-ascii然后再处理string s = "礛祃瑿瑿璷璫璵";
    byte[] vBuffer = Encoding.Default.GetBytes(s);
    s = Encoding.GetEncoding("us-ascii").GetString(vBuffer);
    Console.WriteLine(Encoding.UTF8.GetString(vBuffer));不过楼主的目的是什么?这样转出来有什么意义?
      

  7.   

    to zswang
    因为需要对一些英文文本进行处理,但是里面的乱码对实验有影响,需要将乱码转为正常的字符,因为数G的大数据量,所以不能用UE来人工装换,而是只能放在程序里面来做。例如原文中有这么一句:“procedure to determine protein璵etal ion binding parameters in the presence of excess”,我需要将其转换成“procedure to determine protein-metal ion binding parameters in the presence of excess”,而现在的结果总是“procedure to determine protein?metal ion binding parameters in the presence of excess”:(我出来的结果还是?M?l?C?C?o?c?m
    :(
      

  8.   

    测试代码如下
    public string AsciiToUnicode(byte[] ABuffer)
    {
        char[] vResult = new char[ABuffer.Length];
        for (int i = 0; i < ABuffer.Length; i++)
            vResult[i] = (char)ABuffer[i];
        return new string(vResult);
    }private void button1_Click(object sender, EventArgs e)
    {
        string s = "礛祃瑿瑿璷璫璵";
        byte[] vBuffer = Encoding.Default.GetBytes(s);
        s = AsciiToUnicode(vBuffer);
        Console.WriteLine(s);
    }输出结果:μMμl-C-C-o-c-m
      

  9.   

    string s = "protein璵etal"; //输出protein-metal
      

  10.   

    你的源文件是什么编码的?你用二进制编辑器看一下二进制内容,贴出来我们才好分析。unicode编码还分BE/LE,还有有无BOM的问题