ANSI Unicode,UTF8 转换

    技术2022-05-20  59

     

    CString AnsiToUnicode(std::string str)

    {  

        const char *pstr = str.c_str();

        int len = ::MultiByteToWideChar(CP_ACP, NULL, pstr, -1, NULL, 0);  

        if (len == 0) return L"";  

     

        wchar_t* wszString = new wchar_t[len + 1];

        ::MultiByteToWideChar(CP_ACP, NULL, pstr, -1, wszString, len);

        wszString[len] = '/0'; 

     

        CString strReturn(wszString);

        delete []wszString;

     

        return strReturn;  

    }  

     

    std::string UnicodeToAnsi(const wchar_t* buf)  

    {  

        int len = ::WideCharToMultiByte(CP_ACP, NULL, buf, -1, NULL, 0, NULL, NULL);  

        if (len == 0) return "";  

     

        char* szString = new char[len + 1];

        ::WideCharToMultiByte(CP_ACP, NULL, buf, -1, szString, len, NULL, NULL);  

        szString[len] = '/0'; 

     

        std::string strReturn(szString);

        delete []szString;

     

        return strReturn;  

    }  

     

    CString Utf8ToUnicode(std::string str)  

    {

        const char *pstr = str.c_str();

        int len = ::MultiByteToWideChar(CP_UTF8, NULL, pstr, -1, NULL, 0);  

        if (len == 0) return L"";

     

        wchar_t* wszString = new wchar_t[len + 1];

        ::MultiByteToWideChar(CP_UTF8, NULL, pstr, -1, wszString, len);

        wszString[len] = '/0'; 

     

        CString strReturn(wszString);

        delete []wszString;

     

        return strReturn;  

    }  

     

    std::string UnicodeToUtf8(const wchar_t* buf)  

    {  

        int len = ::WideCharToMultiByte(CP_UTF8, NULL, buf, -1, NULL, 0, NULL, NULL);  

        if (len == 0) return "";  

     

        char* szString = new char[len + 1];

        ::WideCharToMultiByte(CP_UTF8, NULL, buf, -1, szString, len, NULL, NULL);  

        szString[len] = '/0'; 

     

        std::string strReturn(szString);

        delete []szString;

     

        return strReturn;

    }  


    最新回复(0)