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:
- http://www.cplusplus.com/reference/cwchar/swprintf/ (second argument string capacity)
- 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:
- is there way know of 2 interfaces have choose @ compile time?
- 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
Post a Comment