Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <string>
- #include <vector>
- #include <stdexcept>
- // WStrToStr: std::wstring から std::string への変換
- std::string WStrToStr(const std::wstring& wstr) {
- // 最初に変換先のサイズを取得
- int size_needed = WideCharToMultiByte(932, 0, wstr.c_str(), static_cast<int>(wstr.size()), NULL, 0, NULL, NULL);
- if (size_needed <= 0) {
- throw std::runtime_error("WideCharToMultiByte failed to get the size needed.");
- }
- // バッファを確保
- std::vector<char> buffer(size_needed);
- // 実際の変換
- int result = WideCharToMultiByte(932, 0, wstr.c_str(), static_cast<int>(wstr.size()), buffer.data(), size_needed, NULL, NULL);
- if (result <= 0) {
- throw std::runtime_error("WideCharToMultiByte failed during conversion.");
- }
- return std::string(buffer.data(), buffer.size());
- }
- // StrToWStr: std::string から std::wstring への変換
- std::wstring StrToWStr(const std::string& str) {
- // 最初に変換先のサイズを取得
- int size_needed = MultiByteToWideChar(932, 0, str.c_str(), static_cast<int>(str.size()), NULL, 0);
- if (size_needed <= 0) {
- throw std::runtime_error("MultiByteToWideChar failed to get the size needed.");
- }
- // バッファを確保
- std::vector<wchar_t> buffer(size_needed);
- // 実際の変換
- int result = MultiByteToWideChar(932, 0, str.c_str(), static_cast<int>(str.size()), buffer.data(), size_needed);
- if (result <= 0) {
- throw std::runtime_error("MultiByteToWideChar failed during conversion.");
- }
- return std::wstring(buffer.data(), buffer.size());
- }
Advertisement
Add Comment
Please, Sign In to add comment