ABCmo
ABCmo

Reputation: 237

How to convert from std::string to SQLWCHAR*

I'm trying to convert std::string to SQLWCHAR*, but I couldn't find how.

Any brilliant suggestion, please?

Upvotes: 1

Views: 6731

Answers (1)

pqvst
pqvst

Reputation: 4444

One solution would be to simply use a std::wstring in the first place, rather than std::string. With Unicode Character set you can define a wide string literal using the following syntax:

std::wstring wstr = L"hello world";

However, if you would like to stick with std::string then you will need to convert the string to a different encoding. This will depend on how your std::string is encoded. The default encoding in Windows is ANSI (however the most common encoding when reading files or downloading text from websites is usually UTF8).

This answer shows a function for converting a std::string to a std::wstring on Windows (using the MultiByteToWideChar function).

https://stackoverflow.com/a/27296/966782

Note that the answer uses CP_ACP as the input encoding (i.e. ANSI). If your input string is UTF8 then you can change to use CP_UTF8 instead.

Once you have a std::wstring you should be able to easily retrieve a SQLWCHAR* using:

std::wstring::c_str()

Upvotes: 1

Related Questions