C#中文乱码解决:UTF8 转 UNICODE
2013-01-04 13:34
225 查看
XML文件可以采用多种编码,但是经过不同的编码后对于中文会出现乱码问题,比如“骞垮憡涓戦椈”,对于此问题的解决如下:
//方法一: static void Main() { string utf8String = "骞垮憡涓戦椈"; // Create two different encodings. Encoding utf8= Encoding.UTF8; Encoding defaultCode= Encoding.Default; // Convert the string into a byte[]. byte[] utf8Bytes = default.GetBytes(utf8String ); // Perform the conversion from one encoding to the other. byte[] defaultBytes = Encoding.Convert(utf8, defaultCode, utf8Bytes ); // Convert the new byte[] into a char[] and then into a string. // This is a slightly different approach to converting to illustrate // the use of GetCharCount/GetChars. char[] defaultChars = new char[defaultCode.GetCharCount(defaultBytes , 0, defaultBytes .Length)]; defaultCode.GetChars(defaultBytes , 0, defaultBytes .Length, defaultChars , 0); string defaultString = new string(defaultChars ); // Display the strings created before and after the conversion. Console.WriteLine("Original string: {0}", utf8String); Console.WriteLine("Ascii converted string: {0}", defaultString); } //或者方法二: byte[] buffer1 = Encoding.Default.GetBytes(utf8String ); byte[] buffer2 = Encoding.Convert(Encoding.UTF8, Encoding.Default, buffer1, 0, buffer1.Length); string strBuffer = Encoding.Default.GetString(buffer2, 0, buffer2.Length);
相关文章推荐
- C#用正则表达式一键Unicode转UTF8(解决LitJson中文问题)
- 中文乱码的解决之道,理清UTF8,UTF16,GB2312,Big5,Unicode之间的关系
- MFC 多字节和UNICODE的相互转换(解决中文utf8-ASCNII乱码问题)
- 中文乱码的解决之道,理清UTF8,UTF16,GB2312,Big5,Unicode之间的关系
- Linux下MySQL5.6的修改字符集编码为UTF8(解决中文乱码问题)
- .NET连接SAP系统专题:C#调用RFC中文乱码解决办法(十二)
- 【转贴】Linux下MySQL 5.5的修改字符集编码为UTF8(彻底解决中文乱码问题)
- 解决C#中txt文档导入数据库时,中文显示乱码的问题
- MySQL字符集 GBK、GB2312、UTF8区别 解决 MYSQL中文乱码问题
- c#接收中文出现乱码问题解决方法2
- C#中StreamReader读取中文文本出现乱码的解决方法
- windows7下gvim打开UTF8无bom文件中文乱码问题的解决
- C#中WebClient使用DownloadString中文乱码的解决办法
- C#中json中文乱码解决方法
- Linux下MySQL 5.5的修改字符集编码为UTF8(彻底解决中文乱码问题)
- MySQL字符集 GBK、GB2312、UTF8区别 解决 MYSQL中文乱码问题
- Linux下MySQL 5.5/tomact6/Centos 的修改字符集编码为UTF8(彻底解决中文乱码问题)
- Linux下MySQL的修改字符集编码为UTF8(解决中文乱码问题)
- asp.net c# request.QueryString.Form遇到中文乱码的解决方法,可用
- VB6中剪切至剪贴板,粘贴unicode中文出现乱码的解决方法