输出字节缓冲区太小,无法包含编码后的数据,编码“Unicode (UTF-8)”的操作回退“Sys
tem.Text.EncoderReplacementFallback”。
参数名: bytes. static void Main(string[] args)
        {
            byte[] byData;
            char[] charData;
            try
            {
                //charData = "Hello".ToCharArray();
                charData = "你好".ToCharArray();//用中文的字符串抛异常,英文的通过                byData = new byte[charData.Length];
                FileStream aFile = new FileStream("Temp.txt", FileMode.Create);
                Encoder e = Encoding.UTF8.GetEncoder();
                e.GetBytes(charData, 0, charData.Length, byData, 0, true);
                aFile.Seek(0, SeekOrigin.Begin);
                aFile.Write(byData, 0, byData.Length);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadKey();
            }
            return;
        }
把一个char数组转换成byte类型数组,写进FileStream里,中文的转换过来的抛异常!

解决方案 »

  1.   

    为什么要先转化为 char数组呢?可以直接转化为字节数组啊:
    byte[] byData;
                string str= "你好!";
                byData = Encoding.UTF8.GetBytes(str);
      

  2.   

    另外,你的问题在于:转换为char数组的长度 根据编码的不同字节数组的长度也是不一样的,可能需要更多的字节来存储,(比如:Unicode中的char是2字节),所以,错误就在这句:
    byData = new byte[charData.Length];