Jump to content
  • Advertisement
Sign in to follow this  
AsOne

Proper method for converting to and from wchar_t*

This topic is 4786 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

Share this post


Link to post
Share on other sites
You probably want the std::ctype<wchar_t>::widen() function from <locale>.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!