Actually, I had this in mind:
Code:
char wide_to_char(wchar_t wc)
{
char c;
wctomb(&c, wc);
return c;
}
wchar_t char_to_wide(char c)
{
wchar_t wc;
mbtowc(&wc, &c, 1);
return wc;
}
std::string wide_to_string(const std::wstring& ws)
{
using namespace std;
string s;
transform(ws.begin(), ws.end(), back_inserter(s), wide_to_char);
return s;
}
std::wstring string_to_wide(const std::string& s)
{
using namespace std;
wstring ws;
transform(s.begin(), s.end(), back_inserter(ws), char_to_wide);
return ws;
}
If you prefer, the latter two convenience functions could be replaced by a convenience function template:
Code:
template<typename Dest, typename Source, typename Func>
Dest convert(const Source& source, Func func)
{
using namespace std;
Dest dest;
transform(source.begin(), source.end(), back_inserter(dest), func);
return dest;
}
which you can then call such as:
Code:
std::wstring ws;
// ...
std::string s = convert<std::string>(ws, char_to_wide);