MFC unicode 字符传唤

private:
    static char* szResult;
    static wchar_t* wszResult;

public:
    // wchar 转 char
    static const char* wchar_t_Unicode_To_GB2312(wchar_t* str)
    {
        if (szResult)
        {
            delete szResult;
            szResult = NULL;
        }
        int nChar = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);    // 返回结果已包含''所要占用的内存
        nChar = nChar * sizeof(char);
        //char* szResult = new char[nChar];
        szResult = new char[nChar];
        ZeroMemory(szResult, nChar);
        int i = WideCharToMultiByte(CP_ACP, 0, str, -1, szResult, nChar, NULL, NULL);
        int nszResultLen = lstrlenA(szResult);            
        int nszResultSize = sizeof(szResult);            

#if 0    // DEMO
        wchar_t wszTest[] = L"ziwuge";
        wchar_t wszTestNew[] = L"ziwuge博客园";
        int nwszTestLen = lstrlenW(wszTest);            // 6
        int nwszTestNewLen = lstrlenW(wszTestNew);        // 9
        int nwszTestSize = sizeof(wszTest);                // 14
        int nwszTestNewSize = sizeof(wszTestNew);        //    20
        int nChar = WideCharToMultiByte(CP_ACP, 0, wszTestNew, -1, NULL, 0, NULL, NULL);    // 13, 返回结果已包含''所要占用的内存
        nChar = nChar * sizeof(char);                    // 13, 其实这一步可不需要,请见本文前面解释
        char* szResult = new char[nChar];
        ZeroMemory(szResult, nChar);
        int i = WideCharToMultiByte(CP_ACP, 0, wszTestNew, -1, szResult, nChar, NULL, NULL);    // 13
        int nszResultLen = lstrlenA(szResult);            // 12
        int nszResultSize = sizeof(szResult);            // 4
#endif // DEMO

        return szResult;
    }
    // char 转 wchar
    static const wchar_t* char_Gb2312_To_Unicode(const char* szTestNew)
    {
        if (wszResult)
        {
            delete wszResult;
            wszResult = nullptr;
        }

        int nszTestNewLen = lstrlenA(szTestNew);        
        int nszTestNewSize = sizeof(szTestNew);           
        int nWChar = MultiByteToWideChar(CP_ACP, 0, szTestNew, -1, NULL, 0);        // 返回结果已包含''所要占用的内存
        nWChar = nWChar * sizeof(wchar_t);               
        wszResult = new wchar_t[nWChar];
        ZeroMemory(wszResult, nWChar);
        int j = MultiByteToWideChar(CP_ACP, 0, szTestNew, -1, wszResult, nWChar);    
        int nwszResultLen = lstrlenW(wszResult);        
        int nwszResultSize = sizeof(wszResult);           
        return wszResult;

#if 0    //DEMO
        char szTest[] = "ziwuge";
        char szTestNew[] = "ziwuge博客园";
        int nszTestLen = lstrlenA(szTest);                // 6
        int nszTestNewLen = lstrlenA(szTestNew);        // 12
        int nszTestSize = sizeof(szTest);                // 7
        int nszTestNewSize = sizeof(szTestNew);            // 13
        int nWChar = MultiByteToWideChar(CP_ACP, 0, szTestNew, -1, NULL, 0);        // 10, 返回结果已包含''所要占用的内存
        nWChar = nWChar * sizeof(wchar_t);                // 20
        wchar_t* wszResult = new wchar_t[nWChar];
        ZeroMemory(wszResult, nWChar);
        int j = MultiByteToWideChar(CP_ACP, 0, szTestNew, -1, wszResult, nWChar);    // 10
        int nwszResultLen = lstrlenW(wszResult);        // 9
        int nwszResultSize = sizeof(wszResult);            // 4
        return 0;
#endif    //DEMO
    }
    // 方便使用
    static const char* convert(CString& str)
    {
        return wchar_t_Unicode_To_GB2312(str.GetBuffer());
    }
    // 方便使用
    static const char* convert(wchar_t* wszTestNew)
    {
        return wchar_t_Unicode_To_GB2312(wszTestNew);
    }
    // 方便使用
    static const wchar_t* convert(const char* szTestNew)
    {
        return char_Gb2312_To_Unicode(szTestNew);
    }
    // 方便使用
    static const wchar_t* convert(std::string& str)
    {
        return char_Gb2312_To_Unicode(str.c_str());
    }
原文地址:https://www.cnblogs.com/Again/p/6616536.html