C#字符转之UTF8转成Unicode

来源:互联网 发布:淘宝一键复制有坏处吗 编辑:程序博客网 时间:2024/06/03 20:51

用C#写代码时,会经常用到不同编码格式之间的转换。下面代码就是将UTF8编码格式的C# String转换成Unicode编码格式的String。                

仅供参考。


Encoding utf8 = Encoding.UTF8;

Encoding defaultCode = Encoding.Unicode;


 // Convert the string into a byte[].
byte[] utf8Bytes = utf8.GetBytes(str[0].ToString());
// Perform the conversion from one encoding to the other.
byte[] defaultBytes = Encoding.Convert(utf8, defaultCode, utf8Bytes);
char[] defaultChars = new char[defaultCode.GetCharCount(defaultBytes, 0, defaultBytes.Length)];
defaultCode.GetChars(defaultBytes, 0, defaultBytes.Length, defaultChars, 0);

string defaultString = new string(defaultChars);



0 0
原创粉丝点击