Thursday, January 24, 2008

Converting between strings

If I were to have a std::string and wanted to convert it to std::wstring, I usually wrote a function that simply took each character from the source string, and added it to the destination string.

Until someone pointed out to me the obvious solution. It was right in front of me!

std::string s;
std::wstring w = "test";
s.assign( w.begin(), w.end() );
// the vice-versa works too ;)

Now, ain't that easy?

No comments: