1

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:

  1. http://www.cplusplus.com/reference/cwchar/swprintf/ (second argument is the string capacity)
  2. 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:

  1. Is there a way to know which of the 2 interfaces I have to choose at compile time?
  2. 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...

Community
  • 1
  • 1
Antonio
  • 19,451
  • 13
  • 99
  • 197
  • 1
    Never came across [`std::wstring_convert`](http://en.cppreference.com/w/cpp/locale/wstring_convert)? – chris Jul 18 '13 at 07:08
  • 1
    @chris First time hitting my head against this problem, and really couldn't find anything in my many searches! Can you put it in an answer? – Antonio Jul 18 '13 at 07:10
  • `wchar_t* myWCharString[LOCAL_SIZE];` is wrong, should be `wchar_t myWCharString[LOCAL_SIZE];`. `swprintf(errorWString,LOCAL_SIZE,L"%hs",errorString);` is equally wrong, should be `swprintf(errorWString, sizeof errorWstring, L"%hs", errorString);` (notice the whitespace and the use of `sizeof`!) –  Jul 18 '13 at 07:11
  • @chris Do I need C++11? – Antonio Jul 18 '13 at 07:12
  • @H2CO3 Yeah, sorry, that was just a typo, in my program it is correct. Thanks for pointing that out! – Antonio Jul 18 '13 at 07:13
  • @Antonio, Yes, and I haven't used it myself, so I can't say exactly how for each purpose off the bat. I know there are examples of it out there. – chris Jul 18 '13 at 07:15
  • Does your char* string contain utf8 data, or is encoded using code pages? If you dont care about it, then you can use simple for loop to do such conversion. – marcinj Jul 18 '13 at 07:22
  • @marcin_j My input is a "simple" string, no strange coding. I was wondering if there was really no library function to do that... – Antonio Jul 18 '13 at 07:30
  • @chris Unfortunately, it seems that std::wstring_convert is not available for g++ <= 4.7 (sorry, following page is in French, I could not find any other reference http://www.developpez.net/forums/d1235062/c-cpp/cpp/langage/error-wstringconvert-is-not-a-member-of-std/) but the point is that I get an error error: `'wstring_convert' is not a member of 'std'` whatever I include – Antonio Jul 18 '13 at 08:34
  • How about mbstowcs()? http://www.cplusplus.com/reference/cstdlib/mbstowcs/ – marcinj Jul 18 '13 at 12:03
  • @marcin_j It seems to be deprecated under Windows http://msdn.microsoft.com/en-us/library/k1f9b8cy%28v=vs.80%29.aspx and the alternative proposed are not portable... And I am not sure what is exactly a "sequence of multibyte characters" – Antonio Jul 18 '13 at 15:37

1 Answers1

2

Since I can use C++, and efficiency is not an issue, I can use the following:

std::wstring(myCharString,myCharString+strlen(myCharString)).c_str()

And if putting in a wchar_t* was necessary, it could be like this:

strcpy(myWCharString,std::wstring(myCharString,myCharString+strlen(myCharString)).c_str() );


Tested here.

Documentation from basic_string constructor methods:

first, last
    Input iterators to the initial and final positions in a range. 
    The range used is [first,last), which includes all the characters
    between first and last, including the character pointed by first but not
    the character pointed by last.
    The function template argument InputIterator shall be an input iterator type
    that points to elements of a type convertible to charT.
    If InputIterator is an integral type, the arguments are casted to the
    proper types so that signature (5) is used instead.
Antonio
  • 19,451
  • 13
  • 99
  • 197