wchar_t*(或wstring)转char*(或std::string),在unicode情况(有中文)情况下

wchar_t*(或wstring)转char*(或std::string),在unicode情况(有中文)情况下

  C#调用C++时,C#中的char对应于C++中的wchar_t,wchar_t转std::string不太熟悉,根据MSDN上例子搞定
std::string WstringToString(){ 
    wchar_t*unicode = L"Hello,就world.";
    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;
}


你可能感兴趣的:(C++,C#)