两个使用的函数:
1,UTF8转化为Unicode,inline为了编译后更快运行,老用到了,返回字符串为了使用链式表达式
inline WCHAR *UTF8ToUnicode(const char *str) throw()
{
int i = MultiByteToWideChar(CP_UTF8,MB_ERR_INVALID_CHARS,str,-1,NULL,0);
WCHAR *strUnicode=new WCHAR[i];
MultiByteToWideChar(CP_UTF8,MB_ERR_INVALID_CHARS,str,-1,strUnicode,i);
return strUnicode;
delete []strUnicode;
}
{
int i = MultiByteToWideChar(CP_UTF8,MB_ERR_INVALID_CHARS,str,-1,NULL,0);
WCHAR *strUnicode=new WCHAR[i];
MultiByteToWideChar(CP_UTF8,MB_ERR_INVALID_CHARS,str,-1,strUnicode,i);
return strUnicode;
delete []strUnicode;
}
一定要返回WCHAR 或wchar_t类型,否则有些字符就会变成“?”,Unicode(UCS-2)是2个字节宽
2,Unicode转化为UTF8,inline同上意义
inline char *UnicodeToUTF8(const WCHAR* pText) throw()
{
int i= WideCharToMultiByte(CP_UTF8,0,pText,-1,NULL,0,NULL,NULL); //输入缓冲区大小是宽字符数
char *strUTF8 = new char[i];
WideCharToMultiByte(CP_UTF8,0,pText,-1,strUTF8,i,NULL,NULL);
return strUTF8;
delete []strUTF8;
}
{
int i= WideCharToMultiByte(CP_UTF8,0,pText,-1,NULL,0,NULL,NULL); //输入缓冲区大小是宽字符数
char *strUTF8 = new char[i];
WideCharToMultiByte(CP_UTF8,0,pText,-1,strUTF8,i,NULL,NULL);
return strUTF8;
delete []strUTF8;
}
http://xu20cn.blog.51cto.com/274020/66117