Unicode 转 GB2312/UTF8最简单的方式

传统转换方式:

s = @"[\u79FB\u52A8]\u4E00\u5468\u70ED\u95FB\u56DE\u987E\uFF1A\u5F00\u53D1\u5546\u559C\u6B22\u63A8\u8350\u514D\u8D39\u6E38\u620F";
            
Regex reg = new Regex(@"(?i)\\u[a-f0-9]{4}");
Match mat = reg.Match(s);
while (mat.Success)
{
    char c = Convert.ToChar(Convert.ToInt32(mat.Value.Substring(2), 16));
    s = s.Replace(mat.Value, c.ToString());
    mat = reg.Match(s);
}
Console.WriteLine(s);

输出:

[移动]一周热闻回顾:开发商喜欢推荐免费游戏

借助于URLDecode转换:

s = @"[\u79FB\u52A8]\u4E00\u5468\u70ED\u95FB\u56DE\u987E\uFF1A\u5F00\u53D1\u5546\u559C\u6B22\u63A8\u8350\u514D\u8D39\u6E38\u620F";

s = System.Web.HttpUtility.UrlDecode(s.Replace(@"\u", "%u"));
Console.WriteLine(s);


作者:朱会震

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
在 Windows 平台下,可以使用 MultiByteToWideChar 和 WideCharToMultiByte 函数来完成 GB18030/UTF-8 和 Unicode之间的换。 GB18030/UTF-8 Unicode: ``` std::wstring utf8_to_wstring(const std::string& str) { int length = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0); wchar_t* buffer = new wchar_t[length]; MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, buffer, length); std::wstring result(buffer); delete[] buffer; return result; } std::wstring gb_to_wstring(const std::string& str) { int length = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0); wchar_t* buffer = new wchar_t[length]; MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, buffer, length); std::wstring result(buffer); delete[] buffer; return result; } ``` Unicode GB18030/UTF-8: ``` std::string wstring_to_utf8(const std::wstring& str) { int length = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, NULL, 0, NULL, NULL); char* buffer = new char[length]; WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, buffer, length, NULL, NULL); std::string result(buffer); delete[] buffer; return result; } std::string wstring_to_gb(const std::wstring& str) { int length = WideCharToMultiByte(CP_ACP, 0, str.c_str(), -1, NULL, 0, NULL, NULL); char* buffer = new char[length]; WideCharToMultiByte(CP_ACP, 0, str.c_str(), -1, buffer, length, NULL, NULL); std::string result(buffer); delete[] buffer; return result; } ``` 需要注意的是,GB18030/UTF-8 Unicode 时,使用的是 CP_UTF8Unicode GB18030/UTF-8 时,使用的是 CP_ACP 编
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值