Page 1 of 1

std::string to wchar_t*

Posted: Thu Nov 03, 2011 2:16 pm
by Howker
I need to convert a std::string to wchar_t*.
Because I want to use guiEnv->addStaticText(...) to print this string on the screen.
How can I do this?

Re: std::string to wchar_t*

Posted: Thu Nov 03, 2011 3:27 pm
by hybrid
core::stringw(mystdstring.c_str()).c_str() if you don't want to rely on the correct conversion functions for wide chars

Re: std::string to wchar_t*

Posted: Thu Nov 03, 2011 7:25 pm
by Howker
works! thanks.

Re: std::string to wchar_t*

Posted: Fri Nov 04, 2011 7:06 am
by kekar
If you want to stick with C++ STL, you may also do this;

From basic string:

Code: Select all

 
std::string other("foobar");
const std::wstring& ws =  std::wstring(other.begin(), other.end());
const wchar_t* const wchr = wc.c_str();
 
You may also reverse it, replacing std::wstring with std::string.

Re: std::string to wchar_t*

Posted: Fri Nov 04, 2011 9:13 am
by teto
@kekar> interesting way of doing it. Is that always safe ? If you confirm it I might give up on us http://utfcpp.sourceforge.net/ which I currently use and also use iterators.
core::stringw(mystdstring.c_str()).c_str() if you don't want to rely on the correct conversion functions for wide chars
which means some chars are going to be misinterpreted ? What would be the correct irrlicht way (if any) ?

Re: std::string to wchar_t*

Posted: Fri Nov 04, 2011 10:33 am
by hybrid
Actually, for this way it should be safe. All chars should be representable as wchars. So you have no loss. But the other way round will of course make only direct casts from wide to normal chars. And this will break on all extended chars. Don't know how the STL would do this, but in C you have the mb methods for conversion.