<pre class="cpp" name="code">string StrNarrow(const wstring& wstr)
{
int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);// CP_CAP /CP_OEMCP
if (0 == len)
return "";
vector<char> buf;
buf.resize(len);
WideCharToMultiByte(CP_CAP, 0, wstr.c_str(), wstr.size(), &buf[0], len, NULL, NULL);
return string(buf.begin(), buf.end());
}
wstring StrWide(const string& str)
{
int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);
if (0 == len)
return L"";
vector<wchar_t> buf;
buf.resize(len);
MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, &buf[0], len);
return wstring(buf.begin(), buf.end());
}
其实还有一中比较方便的方式:
WideByteToNarrow : std::string(wideStr.begin(), wideStr.end());
NarrowToWideByte : std::wstring(narrowStr.begin(), narrowStr.end());