const wchar_t* to a char array

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
CameroKid
Posts: 71
Joined: Tue Jul 11, 2006 8:54 pm

const wchar_t* to a char array

Post by CameroKid »

How would I go about converting a const wchar_t * variable to a character array?
Ion Dune
Posts: 453
Joined: Mon Nov 12, 2007 8:29 pm
Location: California, USA
Contact:

Post by Ion Dune »

Code: Select all

c8* convertTochar(const wchar_t* w)
{
	size_t count = 255;
	c8* c = (char*)malloc( 255 );
	wcstombs(c, w, count);
	return c;
}
CameroKid
Posts: 71
Joined: Tue Jul 11, 2006 8:54 pm

Post by CameroKid »

perfect, thanks.
stevend
Posts: 114
Joined: Sat Mar 01, 2008 7:18 pm

Post by stevend »

another way would be to use the built in string functions with irrlicht.

wchar_t -> character array

Code: Select all

irr::core::string <char> converter;

converter = some_wide_char;
converter.c_str();
character array -> wchar_t

Code: Select all

irr::core::string <wchar_t> converter;

converter = some_char_array;
converter.c_str();
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

That works for the usual ASCII set only, though, or at least is highly unportable. It's used in several examples, though, and is fine for most simple things. I think I'll have to add some partial template specialization to handle this in a clean way. Just need to make this compile on all platforms somehow...
Post Reply