Page 1 of 1

const wchar_t* to a char array

Posted: Tue Jul 29, 2008 3:49 am
by CameroKid
How would I go about converting a const wchar_t * variable to a character array?

Posted: Tue Jul 29, 2008 4:00 am
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;
}

Posted: Wed Jul 30, 2008 1:23 am
by CameroKid
perfect, thanks.

Posted: Wed Jul 30, 2008 2:52 am
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();

Posted: Wed Jul 30, 2008 10:39 am
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...