Last active
December 10, 2024 20:21
-
-
Save creatorlxd/4e1eea99b0d47a192530585c083d6937 to your computer and use it in GitHub Desktop.
string to wstring and wstring to string
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
#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
Thanks for the code.