Created
May 29, 2016 14:48
-
-
Save max-dark/3cd1b999fcde16cff18e7e6195075bf3 to your computer and use it in GitHub Desktop.
convert std::string to 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
#include <cstdlib> | |
#include <cwchar> | |
#include <string> | |
#include <memory> | |
std::wstring to_wstring(const char* str) { | |
std::unique_ptr<wchar_t[]> tmp = nullptr; | |
size_t sz, len; | |
len = mbstowcs(nullptr, str, 0); | |
sz = len + 1; | |
tmp.reset(new wchar_t[sz]); | |
mbstowcs(tmp.get(), str, sz); | |
return std::wstring(tmp.get()); | |
} | |
std::wstring to_wstring(const std::string& str) { | |
return to_wstring(str.c_str()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment