-
-
Save bqhuyy/1ae719d40ec3788acbba255400a003ea to your computer and use it in GitHub Desktop.
string to wstring and wstring to string
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 <cstdlib> | |
| #include <iostream> | |
| #include <string> | |
| using namespace std; | |
| wstring StringToWString(const string& str) | |
| { | |
| wstring wstr; | |
| size_t size; | |
| wstr.resize(str.length()); | |
| mbstowcs_s(&size,&wstr[0],wstr.size()+1,str.c_str(),str.size()); | |
| return wstr; | |
| } | |
| string WStringToString(const wstring& wstr) | |
| { | |
| string str; | |
| size_t size; | |
| str.resize(wstr.length()); | |
| wcstombs_s(&size, &str[0], str.size() + 1, wstr.c_str(), wstr.size()); | |
| return str; | |
| } | |
| int main() | |
| { | |
| string str; | |
| cin >> str; | |
| wstring wstr; | |
| wstr = StringToWString(str); | |
| wcout << wstr << endl; | |
| //test | |
| cout << "wstring::::" << endl; | |
| // | |
| wcin >> wstr; | |
| str = WStringToString(wstr); | |
| cout << str << endl; | |
| cout << endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment