patterncppModerate
Converting between std::wstring and std::string
Viewed 0 times
stdbetweenwstringandconvertingstring
Problem
While researching ways to convert back and forth between
There were two functions that, to me, looked good. Specifically, these:
However, the double allocation and the need to delete the buffer concern me (performance and exception safety) so I modified them to be like this:
Unit testing indicates that this works in a nice, controlled environment but will this be OK in the vicious and unpredictable world that is my client's computer?
std::wstring and std::string, I found this conversation on the MSDN forums.There were two functions that, to me, looked good. Specifically, these:
std::wstring s2ws(const std::string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
std::string ws2s(const std::wstring& s)
{
int len;
int slength = (int)s.length() + 1;
len = WideCharToMultiByte(CP_ACP, 0, s.c_str(), slength, 0, 0, 0, 0);
char* buf = new char[len];
WideCharToMultiByte(CP_ACP, 0, s.c_str(), slength, buf, len, 0, 0);
std::string r(buf);
delete[] buf;
return r;
}However, the double allocation and the need to delete the buffer concern me (performance and exception safety) so I modified them to be like this:
std::wstring s2ws(const std::string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
std::wstring r(len, L'\0');
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, &r[0], len);
return r;
}
std::string ws2s(const std::wstring& s)
{
int len;
int slength = (int)s.length() + 1;
len = WideCharToMultiByte(CP_ACP, 0, s.c_str(), slength, 0, 0, 0, 0);
std::string r(len, '\0');
WideCharToMultiByte(CP_ACP, 0, s.c_str(), slength, &r[0], len, 0, 0);
return r;
}Unit testing indicates that this works in a nice, controlled environment but will this be OK in the vicious and unpredictable world that is my client's computer?
Solution
I would, and have, redesign your set of functions to resemble casts:
This can have a lot of benefits later when you start having to deal with some 3rd party library's idea of what strings should look like.
std::wstring x;
std::string y = string_cast(x);This can have a lot of benefits later when you start having to deal with some 3rd party library's idea of what strings should look like.
Code Snippets
std::wstring x;
std::string y = string_cast<std::string>(x);Context
StackExchange Code Review Q#419, answer score: 13
Revisions (0)
No revisions yet.