Skip to content

Instantly share code, notes, and snippets.

@siwa32
Created April 20, 2009 19:44
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 siwa32/98704 to your computer and use it in GitHub Desktop.
Save siwa32/98704 to your computer and use it in GitHub Desktop.
string <=> wstring 間の変換
#include <stdlib.h>
#include <string>
/**
* = string <=> wstring 間の変換 =
*
* == HOWTO ==
*
* int main() {
* setlocale(LC_ALL, "Japanese_Japan.932");
* string s = "あいうえお";
* wstring ws = convString(s);
* string ss = convString(ws);
* }
*/
namespace siwa32
{
std::wstring convString(const std::string& input)
{
size_t i;
wchar_t* buffer = new wchar_t[input.size() + 1];
mbstowcs_s(&i, buffer, input.size() + 1, input.c_str(), _TRUNCATE);
std::wstring result = buffer;
delete[] buffer;
return result;
}
std::string convString(const std::wstring& input)
{
size_t i;
char* buffer = new char[input.size() * MB_CUR_MAX + 1];
wcstombs_s(&i, buffer, input.size() * MB_CUR_MAX + 1, input.c_str(), _TRUNCATE);
std::string result = buffer;
delete[] buffer;
return result;
}
}// namespace siwa32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment