2

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 ?

YSC
  • 38,212
  • 9
  • 96
  • 149
A.Adil
  • 29
  • 1
  • 6
  • Which error? Questions seeking debugging help ("**why isn't this code working?**") must include the **desired behavior**, a **specific problem or error** and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. – YSC Jan 26 '17 at 15:13
  • the error is : error C2664: 'wcscmp' : cannot convert parameter 1 from 'char *' to 'const wchar_t *' – A.Adil Jan 26 '17 at 15:22
  • To help people answer your question, you'll need to be more specific about the error. Please [edit] your post to incorporate the exact errors you get from your [mcve] (preferably using copy+paste to avoid transcription errors). – Toby Speight Jan 26 '17 at 18:33

3 Answers3

2

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;
}
Community
  • 1
  • 1
Shaana
  • 326
  • 1
  • 5
0

I'd use C++ tools:

#include <iostream>
#include <string>

// construct a wstring from a string
std::wstring to_wstring(std::string const& str)
{
    const size_t len = ::mbstowcs(nullptr, &str[0], 0);
    if (len == size_t(-1)) {
        throw std::runtime_error("to_wstring()");
    }
    std::wstring result(len, 0);
    ::mbstowcs(&result[0], &str[0], result.size());
    return result;
}


//
// TEST CASES ---
//
const wchar_t key[] = L"764frtfg88fgt320nolmo098vfr"; 
const auto wkey = std::wstring(key);

bool operator==(std::string const& lhs, std::wstring const& rhs)
{
    return to_wstring(lhs) == rhs;
}
bool operator==(std::wstring const& lhs, std::string const& rhs) { return rhs == lhs; }

int main() {
    std::cout << std::boolalpha << ("hello" == wkey) << "\n"
                                << (wkey == "764frtfg88fgt320nolmo098vfr") << "\n";
}

Prints

false
true

Its perks are that it (should) work(s) with non-ASCII characters on both *nix and windows.

YSC
  • 38,212
  • 9
  • 96
  • 149
0

There are other answers already but you could also convert char* to wchat_t* like this.

Declare the following:

const wchar_t *GetWC(const char *c)
{
    const size_t cSize = strlen(c)+1;
    wchar_t* wc = new wchar_t[cSize];
    mbstowcs (wc, c, cSize);

    return wc;
}

Then use it like this:

wchar_t * temprow;
temprow = (wchar_t *)GetWC(row[i]);

/* replace following line with your own */
std::cout << "i " << i << " is " << (wcscmp (key,temprow) != 0) << "\n";

/* avoid memory leak */
free(temprow);

Say thanks to this thread: How to convert char* to wchar_t*?

fripon
  • 101
  • 10