您的位置:首页 > 编程语言 > C#

C#字符转之UTF8转成Unicode

2016-12-02 19:17 204 查看
用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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: