Created
April 20, 2009 19:44
-
-
Save siwa32/98704 to your computer and use it in GitHub Desktop.
string <=> 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 <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