Search code examples
c++pointerswchar-twchar

Compare C-string of types char* and wchar_t*


I have a key like:

wchar_t key[] = L"764frtfg88fgt320nolmo098vfr"; 

and a char* row[i] returned by a query from a Database.

I'd like to compare my Key with row[i]. I tried with

wcscmp (key,row[i]) != 0)

but it gives me an error. Any suggestions ?


Solution

  • This might help: C++ Convert string (or char*) to wstring (or wchar_t*)

    As a summary:

    #include <string>
    
    wchar_t Key[] = L"764frtfg88fgt320nolmo098vfr";
    std::wstring k(Key);
    
    const char* text = "test"; // your row[i]
    std::string t(text);
    // only works well if the string being converted contains only ASCII characters.
    std::wstring a(t.begin(), t.end()); 
    
    if(a.compare(k) == 0)
    {   
        std::cout << "same" << std::endl;
    }