C#及C++中文字符与Unicode编码解密

c#源码

汉字转Unicode编码如 左侧围=》\\u5de6\\u4fa7\\u56f4

        /// <summary>
        /// 汉字转换为Unicode编码
        /// </summary>
        /// <param name="str">要编码的汉字字符串</param>
        /// <returns>Unicode编码的的字符串</returns>
        public  string ToUnicode(string str)
        {
            byte[] bts = Encoding.Unicode.GetBytes(str);
            string r = "";
            for (int i = 0; i < bts.Length; i += 2) r += "\\u" + bts[i + 1].ToString("x").PadLeft(2, '0') + bts[i].ToString("x").PadLeft(2, '0');
            return r;

        }

Unicode编码转换为汉字 \\u5de6\\u4fa7\\u56f4=》左侧围

         /// <summary>
        /// Unicode编码转换为汉字
        /// </summary>
        /// <param name="unicodeString">Unicode编码的的字符串</param>
        /// <returns>汉字字符串</returns>
        public static string ConvertToChinese(string unicodeString)
        {
            string[] unicodeCodes = unicodeString.Split('\\', 'u');
            StringBuilder chineseString = new StringBuilder();

            foreach (string code in unicodeCodes)
            {
                if (!string.IsNullOrEmpty(code))
                {
                    int unicodeValue = Convert.ToInt32(code, 16);
                    chineseString.Append(char.ConvertFromUtf32(unicodeValue));
                }
            }

            return chineseString.ToString();
        }

 C++

//中转Unicode
std::string ConvertUnicodeEscape(const std::string& unicode_str)
{
	//std::wstring unicode_wstr = String2Wstring(unicode_str);
	std::wstring unicode_wstr = StringToWString(unicode_str);
	std::stringstream ss;
	for (auto iter = unicode_wstr.cbegin(); iter != unicode_wstr.cend(); ++iter) {
		if (*iter < 128) {
			ss << (char)*iter;
		}
		else {
			ss << "\\u" << std::hex << std::setfill('0') << std::setw(4) << (int)*iter;
		}
	}
	return ss.str();
}

//Unicode转中
std::string Unescape(const std::string& input) 
{
	wstring wresult;
	for (size_t i = 0; i < input.length(); ) {
		if (input[i] == '\\' && input[i + 1] == 'u') {
			std::string code = input.substr(i + 2, 4);
			wchar_t unicode = stoi(code, nullptr, 16);
			wresult += unicode;
			i += 6;
		}
		else {
			wresult += input[i++];
		}
	}
	wstring_convert<codecvt_utf8<wchar_t>> conv;
	//std::string result = conv.to_bytes(wresult);
	std::string result = Wstring2String(wresult);
	return res
}


std::string Wstring2String(std::wstring wstr)
{
	// support chinese
	std::string res;
	int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), nullptr, 0, nullptr, nullptr);
	if (len <= 0) {
		return res;
	}
	char* buffer = new char[len + 1];
	if (buffer == nullptr) {
		return res;
	}
	WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, nullptr, nullptr);
	buffer[len] = '\0';
	res.append(buffer);
	delete[] buffer;
	return res;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值