Created
March 7, 2013 07:13
-
-
Save chenzx/5106140 to your computer and use it in GitHub Desktop.
wstring <=> string convert utils
This file contains hidden or 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
| #include<cassert> | |
| #include<cstdio> | |
| #include<string> | |
| #include<map> | |
| using namespace std; | |
| std::wstring string2wstring(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 wstring2string(const std::wstring& s) | |
| { | |
| int len; | |
| int slength = (int)s.length() + 1; | |
| len = WideCharToMultiByte(CP_ACP, 0, s.c_str(), slength, 0, 0, NULL, NULL); | |
| char* buf = new char[len]; | |
| WideCharToMultiByte(CP_ACP, 0, s.c_str(), slength, buf, len, NULL, NULL); | |
| std::string r(buf); | |
| delete[] buf; | |
| return r; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment