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.

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 on other sites
Quote:
 Original post by AsOneI 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 on other sites
Don't bother converting just use std:wstring and std::wstringstream to do all your formatting

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

Share on other sites
Ah there we go, thanks for the help.

1. 1
Rutin
25
2. 2
3. 3
4. 4
JoeJ
17
5. 5

• 11
• 14
• 11
• 9
• 9
• Forum Statistics

• Total Topics
631756
• Total Posts
3002115
×