Unicode 转 GB2312/UTF8最简单的方式

时间:2023-01-05 23:59:27

传统转换方式:

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


作者:朱会震