I am trying to convert a char* string to wchar_t*. I have seen this question has been asked many times, with no resolving/portable answer/solution.
As suggested here, swprintf seemed the right solution to me, but I found out there exist two versions out there!! Namely:
- http://www.cplusplus.com/reference/cwchar/swprintf/ (second argument is the string capacity)
- http://msdn.microsoft.com/en-us/library/ybk95axf%28v=vs.71%29.aspx (second argument is already the format string)
My program would look like this:
const unsigned int LOCAL_SIZE = 256;
char* myCharString = "Hello world!";
wchar_t myWCharString[LOCAL_SIZE];
And at this point:
swprintf(myWCharString,LOCAL_SIZE,L"%hs",myCharString );
or:
swprintf(myWCharString,L"%hs",myCharString );
And switching compiler (mingw 4.5.2 <-> mingw 4.7.2) I did get that different version were implemented, so in one case an error at compilation time! My questions:
- Is there a way to know which of the 2 interfaces I have to choose at compile time?
- Is there an alternative, portable way to transform a char* string in a wchar_t*? I can pass through C++ std libraries (no C++11) for example if necessary
Edit
std::wstring_convert
doesn't seem to be available for my compiler (neither 4.5.2 nor 4.7.2, including #include <locale>
I will check out later if I can use Boost Format Library to try to solve this...