Skip to content

Instantly share code, notes, and snippets.

@shiyuugohirao
Last active May 1, 2021 22:13
Show Gist options
  • Save shiyuugohirao/5fb074dd346f96945136537bc6e2e7b9 to your computer and use it in GitHub Desktop.
Save shiyuugohirao/5fb074dd346f96945136537bc6e2e7b9 to your computer and use it in GitHub Desktop.
simple wstring & string converter in c++.
// 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());
}
@shiyuugohirao
Copy link
Author

refer to
https://nekko1119.hatenablog.com/entry/2017/01/02/054629

std::codecvt std::wstring_convert are deprecated in c++17.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment