UniCode 下char*转CString ,利用MultiByteToWideChar进行转换,中文乱码的解决方案

使用A2W,A2T这些宏函数是常见的方法,但是中文会乱码,所以采用MultiByteToWideChar进行转换

//计算char *数组大小,以字节为单位,一个汉字占两个字节
    int charLen = strlen(sText);
    //计算多字节字符的大小,按字符计算。
    int len = MultiByteToWideChar(CP_ACP,0,sText,charLen,NULL,0);
    //为宽字节字符数组申请空间,数组大小为按字节计算的多字节字符大小
    TCHAR *buf = new TCHAR[len + 1];
    //多字节编码转换成宽字节编码
    MultiByteToWideChar(CP_ACP,0,sText,charLen,buf,len);
    buf[len] = '\0'; //添加字符串结尾,注意不是len+1
    //将TCHAR数组转换为CString
    CString pWideChar;
    pWideChar.Append(buf);
    //删除缓冲区
    delete []buf;
原文地址:https://www.cnblogs.com/junyuz/p/2766737.html