Windows下简体中文与繁体中文(BIG5与GBK)相互转换方案
//繁体转换成简体
__inline static
std::string TraditionalToSimplified(const std::string& big5)
{
if (!big5.empty())
{
LCID lcid = MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_CHINESE_PRC);
std::string gbk(LCMapStringA(lcid, LCMAP_SIMPLIFIED_CHINESE, (LPCSTR)big5.c_str(), -1, NULL, 0), '\0');
return gbk.substr(0, LCMapStringA(lcid, LCMAP_SIMPLIFIED_CHINESE, (LPCSTR)big5.c_str(), -1, (LPSTR)gbk.data(), (int)gbk.size()) - 1);
}
return ("");
}
//繁体转换成简体
__inline static
std::wstring TraditionalToSimplified(const std::wstring& big5)
{
if (!big5.empty())
{
LCID lcid = MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_CHINESE_PRC);
std::wstring gbk(LCMapStringW(lcid, LCMAP_SIMPLIFIED_CHINESE, (LPCWSTR)big5.c_str(), -1, NULL, 0), L'\0');
return gbk.substr(0, LCMapStringW(lcid, LCMAP_SIMPLIFIED_CHINESE, (LPCWSTR)big5.c_str(), -1, (LPWSTR)gbk.data(), (int)gbk.size()) - 1);
}
return (L"");
}
//简体转换成繁体
__inline static
std::string SimplifiedToTraditional(const std::string& gbk)
{
if (!gbk.empty())
{
LCID lcid = MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_CHINESE_PRC);
std::string big5(LCMapStringA(lcid, LCMAP_TRADITIONAL_CHINESE, (LPCSTR)gbk.c_str(), -1, NULL, 0), '\0');
return big5.substr(0, LCMapStringA(lcid, LCMAP_TRADITIONAL_CHINESE, (LPCSTR)gbk.c_str(), -1, (LPSTR)big5.data(), (int)big5.size()) - 1);
}
return ("");
}
//简体转换成繁体
__inline static
std::wstring SimplifiedToTraditional(const std::wstring& gbk)
{
if (!gbk.empty())
{
LCID lcid = MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_CHINESE_PRC);
std::wstring big5(LCMapStringW(lcid, LCMAP_TRADITIONAL_CHINESE, (LPCWSTR)gbk.c_str(), -1, NULL, 0), L'\0');
return big5.substr(0, LCMapStringW(lcid, LCMAP_TRADITIONAL_CHINESE, (LPCWSTR)gbk.c_str(), -1, (LPWSTR)big5.data(), (int)big5.size()) - 1);
}
return (L"");
}