2

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

Any brilliant suggestion, please?

ABCmo
  • 239
  • 1
  • 6
  • 16
  • Refer to the following post: [Post][1] [1]: http://stackoverflow.com/questions/11275225/converting-stdwstring-to-sqlwchar – Chase W. Mar 12 '14 at 21:58
  • @user3037661, that's `std::wstring` – ABCmo Mar 12 '14 at 21:58
  • Shoot good catch, never mind then – Chase W. Mar 12 '14 at 22:00
  • What you need to do is to convert your multi-byte string to a wide-char string. This depends on how your std::string is encoded (e.g UTF8?). Windows has a useful function called `MultiByteToWideChar`. Other platforms (or string libraries) will have equivalent functions. – pqvst Mar 12 '14 at 22:02
  • @pbergqvist, I'm using Unicode Character set – ABCmo Mar 12 '14 at 22:05

1 Answers1

1

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()
Community
  • 1
  • 1
pqvst
  • 4,344
  • 6
  • 33
  • 42