[DllImport("Kernel32", CharSet = CharSet.Auto)]
static extern Int32 MultiByteToWideChar(
UInt32 codePage,
UInt32 dwFlags,
[In, MarshalAs(UnmanagedType.LPStr)] String lpMultiByteStr,
Int32 cbMultiByte,
[Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpWideCharStr,
Int32 cchWideChar);
private string ConvertToUnicode( string str, uint codepage)//utf8 to unicode
{
int l = str.Length;
int i = 0;
i = MultiByteToWideChar( codepage, 0, str, -1, null, 0);
StringBuilder wideStr = new StringBuilder(i);
i = MultiByteToWideChar( codepage, 0, str, -1, wideStr,
wideStr.Capacity);
string s = wideStr.ToString();
return s;
}
若将str(utf-8)转str1 (unicode):
string str1 = ConvertToUnicode(str, 65001);//65001 utf-8 codepage
没完全测试,不知是否有误,望指正
本文详细介绍了如何使用C#将UTF-8编码的字符串转换为Unicode编码的字符串,包括转换函数的实现原理及示例代码。
466

被折叠的 条评论
为什么被折叠?



