VC下utf8到UNICODE的编码转换

来源:互联网 发布:高薪网络兼职 编辑:程序博客网 时间:2024/05/22 00:25
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的转换