admin 发表于 2023-10-29 20:11:44

wchar_t*转std::string

std::string wideChar2string(wchar_t* pWCStrKey)
{
        std::wstring wstr(pWCStrKey);
        std::string str(wstr.length(), ' ');
        std::copy(wstr.begin(), wstr.end(), str.begin());
        return str;
}

admin 发表于 2023-10-29 20:38:44

上面的中文乱码。
std::string wideChar2string(wchar_t* unicode)
{
        int len;
        len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL);
        char* szUtf8 = (char*)malloc(len + 1);
        memset(szUtf8, 0, len + 1);
        WideCharToMultiByte(CP_OEMCP, 0, (const wchar_t*)unicode, -1, szUtf8, len, NULL, NULL);
        //WideCharToMultiByte(CP_UTF8, 0, (const wchar_t*)unicode, -1, szUtf8, len, NULL, NULL);
        return szUtf8;
        std::string str1(szUtf8);
        return str1;
}
页: [1]
查看完整版本: wchar_t*转std::string