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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在计算机编程中,C和C++都是非常重要的编程语言。它们都是被广泛应用于系统开发和高性能应用程序的常用语言。 C是一种被广泛使用的、面向过程的编程语言。它由Dennis Ritchie在20世纪70年代初开发,一开始被用于UNIX操作系统的开发。C具有简洁的语法结构和丰富的函数库,使得它非常适合用于系统级编程和嵌入式设备开发。C还可以编写高效的代码,并具有较少的内存占用,因此它对于资源有限的设备非常合适。许多其他编程语言,如C++、Java和Python等,都是在C的基础上开发的。 C++是在C语言的基础上扩展而来的编程语言。它由Bjarne Stroustrup在20世纪80年代开发,旨在为C语言添加面向对象的特性。C++继承了C的简洁性和高效性,并添加了类、继承、封装和多态等面向对象的功能。C++还具有强大的标准模板库(STL),提供了许多现成的容器和算法,使得程序开发更加快速和简便。C++广泛应用于游戏开发、图形界面以及大规模软件系统等领域。 虽然C和C++在某些方面相似,但它们也有一些重要的区别。C语言更加简洁,具有更小的语法和更少的特性,因此更加适合编写较低级别的、对性能要求较高的程序。而C++则更加强大和灵活,具有更多的特性和功能,使得它更适合开发大型软件系统和应用程序。 总体而言,C和C++都是非常重要的编程语言。选择使用哪个取决于项目的具体需求和个人的偏好。无论是选择C还是C++,掌握这些编程语言的基本概念和语法结构都是非常有益的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值