Skip to content

Instantly share code, notes, and snippets.

@Tosainu
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tosainu/f36581dc8cf624ddfbc5 to your computer and use it in GitHub Desktop.
Save Tosainu/f36581dc8cf624ddfbc5 to your computer and use it in GitHub Desktop.
#if !defined HELPER_HPP
#define HELPER_HPP
#include <codecvt>
#include <locale>
#include <string>
namespace helper {
std::string to_string(const std::wstring& src) {
static std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
return conv.to_bytes(src);
}
std::wstring to_wide(const std::string& src) {
static std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
return conv.from_bytes(src);
}
}
#endif
#include <iostream>
#include "helper.hpp"
auto main() -> int {
const std::string str("std::string!!");
const std::wstring wstr(L"std::wstring!!");
const std::string from_wstring = helper::to_string(wstr);
const std::wstring from_string = helper::to_wide(str);
std::cout << from_wstring << '\n';
std::wcout << from_string << '\n';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment