Sign in to follow this  
AsOne

Proper method for converting to and from wchar_t*

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this