Last active
May 1, 2021 22:13
-
-
Save shiyuugohirao/5fb074dd346f96945136537bc6e2e7b9 to your computer and use it in GitHub Desktop.
simple wstring & string converter in c++.
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
// wstringUtils.h | |
// https://gist.github.com/shiyuugohirao/5fb074dd346f96945136537bc6e2e7b9 | |
#pragma once | |
#include <stringapiset.h> | |
#include <WinBase.h> | |
#include <system_error> | |
#include <vector> | |
static std::wstring to_wstring(std::string const& src) | |
{ | |
auto const dest_size = ::MultiByteToWideChar(CP_ACP, 0U, src.data(), -1, nullptr, 0U); | |
std::vector<wchar_t> dest(dest_size, L'\0'); | |
if (::MultiByteToWideChar(CP_ACP, 0U, src.data(), -1, dest.data(), dest.size()) == 0) { | |
throw std::system_error{ static_cast<int>(::GetLastError()), std::system_category() }; | |
} | |
dest.resize(std::char_traits<wchar_t>::length(dest.data())); | |
dest.shrink_to_fit(); | |
return std::wstring(dest.begin(), dest.end()); | |
} | |
static std::string to_string(std::wstring const& src) | |
{ | |
auto const dest_size = ::WideCharToMultiByte(CP_ACP, 0U, src.data(), -1, nullptr, 0, nullptr, nullptr); | |
std::vector<char> dest(dest_size, '\0'); | |
if (::WideCharToMultiByte(CP_ACP, 0U, src.data(), -1, dest.data(), dest.size(), nullptr, nullptr) == 0) { | |
throw std::system_error{ static_cast<int>(::GetLastError()), std::system_category() }; | |
} | |
dest.resize(std::char_traits<char>::length(dest.data())); | |
dest.shrink_to_fit(); | |
return std::string(dest.begin(), dest.end()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
refer to
https://nekko1119.hatenablog.com/entry/2017/01/02/054629
std::codecvt std::wstring_convert are deprecated in c++17.