void utf8tow(wchar_t *pwstr,size_t len,const char *str)
{
if(str)
{
size_t nu = strlen(str);
size_t n =(size_t)MultiByteToWideChar(CP_UTF8,0,( const char *)str,(int )nu,NULL,0);
if(n>=len) n=len-1;
MultiByteToWideChar(CP_UTF8,0,( const char *)str,(int )nu,pwstr,(int)n);
pwstr[n]=0;
}
}
三个参数分别代表:输出wchar_t字符串,输入字符串长度,输入字符串以上这个函数经过测试稳定正确。把两处MultiByteToWideChar的第一个参数改为CP_ACP就可以实现ANSI到UNICODE的转换
本文介绍了一个稳定的UTF-8编码转换为宽字符(wchar_t)的C/C++函数。该函数通过MultiByteToWideChar函数实现,适用于Windows平台。文章提供了完整的代码示例,并说明了如何修改实现ANSI到UNICODE的转换。
31

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



