c++ - Understanding which swprintf will be used (or again, convert a char* string to wchar_t*) -


i trying convert char* string wchar_t*. have seen question has been asked many times, no resolving/portable answer/solution.

as suggested here, swprintf seemed right solution me, found out there exist 2 versions out there!! namely:

  1. http://www.cplusplus.com/reference/cwchar/swprintf/ (second argument string capacity)
  2. http://msdn.microsoft.com/en-us/library/ybk95axf%28v=vs.71%29.aspx (second argument format string)

my program this:

const unsigned int local_size = 256; char* mycharstring = "hello world!"; wchar_t mywcharstring[local_size]; 

and @ 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) did different version implemented, in 1 case error @ compilation time! questions:

  1. is there way know of 2 interfaces have choose @ compile time?
  2. is there alternative, portable way transform char* string in wchar_t*? can pass through c++ std libraries (no c++11) example if necessary

edit

std::wstring_convert doesn't seem available compiler (neither 4.5.2 nor 4.7.2, including #include <locale>

i check out later if can use boost format library try solve this...

since can use c++, , efficiency not issue, can use following:

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

and if putting in wchar_t* necessary, this:

strcpy(mywcharstring,std::wstring(mycharstring,mycharstring+strlen(mycharstring)).c_str() ); 


tested here.

documentation basic_string constructor methods:

first, last     input iterators initial , final positions in range.      range used [first,last), includes characters     between first , last, including character pointed first not     character pointed last.     function template argument inputiterator shall input iterator type     points elements of type convertible chart.     if inputiterator integral type, arguments casted     proper types signature (5) used instead. 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -