以前書いたコードのバージョンアップ版です。
仕様は以前と同じです。若干コードがスマートになっている・・・かもしれません。
std::string UnicodeToMultiByte(const std::wstring& Source, UINT CodePage = CP_ACP, DWORD Flags = 0);
std::string UnicodeToMultiByte(const std::wstring& Source, UINT CodePage, DWORD Flags)
{
std::vector<char> Dest(::WideCharToMultiByte(CodePage, Flags, Source.c_str(), static_cast<int>(Source.size()), NULL, 0, NULL, NULL));
return std::string(Dest.begin(),
Dest.begin() + ::WideCharToMultiByte(CodePage, Flags, Source.c_str(), static_cast<int>(Source.size()), &Dest[0], static_cast<int>(Dest.size()), NULL, NULL));
}
std::wstring MultiByteToUnicode(const std::string& Source, UINT CodePage = CP_ACP, DWORD Flags = 0);
std::wstring MultiByteToUnicode(const std::string& Source, UINT CodePage, DWORD Flags)
{
std::vector<wchar_t> Dest(::MultiByteToWideChar(CodePage, Flags, Source.c_str(), static_cast<int>(Source.size()), NULL, 0));
return std::wstring(Dest.begin(),
Dest.begin() + ::MultiByteToWideChar(CodePage, 0, Source.c_str(), static_cast<int>(Source.size()), &Dest[0], static_cast<int>(Dest.size())));
}
ところで、C++標準ライブラリを使ってプログラムするとき、wchar_tをメインに使おうとすると、ファイルが開けなくてこまります。
なんでstd::fstreamはファイル名にconst char *しか指定できないんでしょうね。
おかげでこんな変換関数を使うハメに陥ってしまいます。