Created
May 31, 2013 10:28
-
-
Save 1901/5684151 to your computer and use it in GitHub Desktop.
std::string和std::wstring之间相互转换
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 把一个wstring转化为string | |
std::string& to_string(std::string& dest, std::wstring const & src) | |
{ | |
std::setlocale(LC_CTYPE, ""); | |
size_t const mbs_len = wcstombs(NULL, src.c_str(), 0); | |
std::vector<char> tmp(mbs_len + 1); | |
wcstombs(&tmp[0], src.c_str(), tmp.size()); | |
dest.assign(tmp.begin(), tmp.end() - 1); | |
return dest; | |
} | |
// 把一个string转化为wstring | |
std::wstring& to_wstring(std::wstring& dest, std::string const & src) | |
{ | |
// std::setlocale(LC_CTYPE, ""); | |
std::setlocale(LC_CTYPE, "zh_CN"); | |
size_t const wcs_len = mbstowcs(NULL, src.c_str(), 0); | |
std::vector<wchar_t> tmp(wcs_len + 1); | |
mbstowcs(&tmp[0], src.c_str(), src.size()); | |
dest.assign(tmp.begin(), tmp.end() - 1); | |
return dest; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment