Proper method for converting to and from wchar_t*

Started by
3 comments, last by AsOne 18 years, 10 months ago
Hello everyone, I'm currently using the Irrlicht game engine right now. All the text in the engine uses wchar_t* so text that the gui will display for example must be wchar_t*. This has become somewhat of a pain, as when I have to convert a numeric value to a string I have to use streams and then convert to wchar_t* and then after I'm done with the string I have to delete it myself, It seems like quite a heavy procedure just for writing text to the screen. For instance this code displays the fps as the window title.

wchar_t *CMain::ToWideString(const std::string str) {
	int length = str.size() + 1;
	wchar_t *buffer = new wchar_t[length];
	::mbstowcs(buffer, str.c_str(), length);
	return buffer;
}

void CMain::SetWindowCaption() {
	std::ostringstream sstream;
	sstream << driver->getFPS();
	std::string string = "Demo (" + sstream.str() + ")";
	wchar_t *buffer = ToWideString(string);
	device->setWindowCaption(buffer);
	delete[] buffer;
}


I also don't really like the fact that I'm using ::mbstowcs which is depreciated. Anyways is there a simpler way I can do this (converting numeric values to wchar_t*) without using old depreciated functions if possible? Thanks for any help in advance.
Advertisement
Quote:Original post by AsOne
I also don't really like the fact that I'm using ::mbstowcs which is depreciated. Anyways is there a simpler way I can do this (converting numeric values to wchar_t*) without using old depreciated functions if possible?

Thanks for any help in advance.


string & ostringstream are type aliases (typedefs) for class templates, the real ones are called basic_string and basic_ostringstream, if you want the wide character version of these then there is a type alias for them wstring, and wostringstream.
Don't bother converting just use std:wstring and std::wstringstream to do all your formatting
You probably want the std::ctype<wchar_t>::widen() function from <locale>.

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Ah there we go, thanks for the help.

This topic is closed to new replies.

Advertisement