代码拉取完成,页面将自动刷新
#include "XUtil.h"
#ifdef WIN32
#pragma warning(push)
#pragma warning(disable: 28251)
extern "C" __declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int cp, unsigned long flags, const char* str, int cbmb, wchar_t* widestr, int cchwide);
extern "C" __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int cp, unsigned long flags, const wchar_t* widestr, int cchwide, char* str, int cbmb, const char* defchar, int* used_default);
#pragma warning(pop)
std::wstring XUtil::UTF8ToWString(const std::string& utf8str)
{
if (utf8str.empty())
return std::wstring();
int cbMultiByte = static_cast<int>(utf8str.size());
int req = MultiByteToWideChar(65001, 0, utf8str.data(), cbMultiByte, nullptr, 0);
std::wstring res(req, 0);
MultiByteToWideChar(65001, 0, utf8str.data(), cbMultiByte, &res[0], req);
return res;
}
std::string XUtil::WStringToUTF8(const std::wstring &wstr)
{
if (wstr.empty())
return std::string();
int cbMultiByte = static_cast<int>(wstr.size());
int req = WideCharToMultiByte(65001, 0, wstr.data(), cbMultiByte, nullptr, 0, nullptr, nullptr);
std::string res(req, 0);
WideCharToMultiByte(65001, 0, wstr.data(), cbMultiByte, &res[0], req, nullptr, nullptr);
return res;
}
#endif // DEBUG
#ifdef __linux__
std::wstring XUtil::UTF8ToWString(const std::string &mbstr)
{
std::wstring wstr;
if (mbstr.empty())
return wstr;
// Get the required size for the wide string
size_t size = mbstowcs(nullptr, mbstr.c_str(), 0);
if (size == static_cast<size_t>(-1))
{
// Error handling
return wstr;
}
wstr.resize(size);
// Perform the conversion
size = mbstowcs(&wstr[0], mbstr.c_str(), size);
if (size == static_cast<size_t>(-1))
{
// Error handling
return std::wstring();
}
return wstr;
}
std::string XUtil::WStringToUTF8(const std::wstring &wstr)
{
std::string mbstr;
if (wstr.empty())
return mbstr;
// Get the required size for the multi-byte string
size_t size = wcstombs(nullptr, wstr.c_str(), 0);
if (size == static_cast<size_t>(-1))
{
// Error handling
return mbstr;
}
mbstr.resize(size);
// Perform the conversion
size = wcstombs(&mbstr[0], wstr.c_str(), size);
if (size == static_cast<size_t>(-1))
{
// Error handling
return std::string();
}
return mbstr;
}
#endif
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。