c++ unicode转中文

发布时间:2025-12-09 20:14:00 浏览次数:4

方式一:

#include <string>#include <locale>#include <codecvt>//unicode转中文 (\u4f60\u597d\u554a ==》 你好啊)string unicodeToCHI(string &unicodeStr){string uStr, value;int index, len;wstring_convert<codecvt_utf8<char32_t>, char32_t> converter;if (unicodeStr.length() <= 0){return "";}//unicode转utf8while (1){index = unicodeStr.find("\\u");if (index<0 || index>unicodeStr.length())break;//转换需要的地方uStr = unicodeStr.substr(index + 2, 4);for (size_t i = 0; i < uStr.length();){char32_t uhan = strtol(uStr.substr(i, 4).c_str(), nullptr, 16);value = converter.to_bytes(uhan);i += 4;}//在原字符串中替换掉unicodeStr.replace(index, 6, value);}//utf8转ANSIlen = ::MultiByteToWideChar(CP_UTF8, 0, unicodeStr.c_str(), -1, NULL, 0);WCHAR* wStr = new WCHAR[len + 1];memset(wStr, 0, sizeof(wStr));len = MultiByteToWideChar(CP_UTF8, 0, unicodeStr.c_str(), -1, wStr, len);len = WideCharToMultiByte(CP_ACP, 0, wStr, -1, NULL, 0, NULL, NULL);char *szANSI = new char[len + 1];memset(szANSI, 0, sizeof(szANSI));WideCharToMultiByte(CP_ACP, 0, wStr, -1, szANSI, len, NULL, NULL);unicodeStr = szANSI;delete[]wStr;wStr = NULL;return unicodeStr;}

方式二:

#include <atlstr.h>string unicodeToCHI2(string &unicodeStr){int index;CString cstr;int nValue = 0;WCHAR * pWchar;wchar_t* szHex;char strchar[6] = { '0', 'x', '\0' };if (unicodeStr.length() <= 0){return "";}while (1){int index = unicodeStr.find("\\u");if (index <0 || index>unicodeStr.length())break;strchar[2] = unicodeStr[index+2];strchar[3] = unicodeStr[index + 3];strchar[4] = unicodeStr[index + 4];strchar[5] = unicodeStr[index + 5];USES_CONVERSION;szHex = A2W(strchar);StrToIntExW(szHex, STIF_SUPPORT_HEX, &nValue);pWchar = (WCHAR*)& nValue;cstr = pWchar;//替换掉unicode字符串unicodeStr.replace(index, 6, cstr);}return unicodeStr;}

注意:目前好像发现有些字符转换不过来,比如 \u0456

需要做网站?需要网络推广?欢迎咨询客户经理 13272073477