std::string to wchar_t*

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Howker
Posts: 51
Joined: Thu Jan 22, 2009 4:40 pm

std::string to wchar_t*

Post 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?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: std::string to wchar_t*

Post by hybrid »

core::stringw(mystdstring.c_str()).c_str() if you don't want to rely on the correct conversion functions for wide chars
Howker
Posts: 51
Joined: Thu Jan 22, 2009 4:40 pm

Re: std::string to wchar_t*

Post by Howker »

works! thanks.
kekar
Posts: 13
Joined: Mon Dec 06, 2010 12:07 pm

Re: std::string to wchar_t*

Post 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.
teto
Posts: 159
Joined: Thu Dec 03, 2009 9:37 pm
Location: /home
Contact:

Re: std::string to wchar_t*

Post 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) ?
Using trunk with mingw/gcc 4.6, Windows 7 64 bits driver opengl
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: std::string to wchar_t*

Post 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.
Post Reply