C# Unicode编码解码
public static class CommpnHelpEx
{
/// <summary>
/// unicode编码
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string ToUnicodeString(this string str)
{
StringBuilder strResult = new StringBuilder();
if (!string.IsNullOrEmpty(str))
{
for (int i = 0; i < str.Length; i++)
{
strResult.Append("\\u");
strResult.Append(((int)str[i]).ToString("x"));
}
}
return strResult.ToString();
}
/// <summary>
/// unicode解码
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string FromUnicodeString(this string str)
{
//最直接的方法Regex.Unescape(str);
StringBuilder strResult = new StringBuilder();
if (!string.IsNullOrEmpty(str))
{
string[] strlist = str.Replace("\\", "").Split('u');
try
{
for (int i = 1; i < strlist.Length; i++)
{
int charCode = Convert.ToInt32(strlist[i], 16);
strResult.Append((char)charCode);
}
}
catch (FormatException ex)
{
return Regex.Unescape(str);
}
}
return strResult.ToString();
}
}
使用方式:
string str = "我是共.产.党";
string unicodeStr = str.ToUnicodeString();
string chStr = unicodeStr.FromUnicodeString();
C# Unicode编码解码的更多相关文章
- Unicode编码解码在线转换工具
// Unicode编码解码在线转换工具 Unicode 是基于通用字符集(Universal Character Set)的标准来发展,并且同时也以书本的形式(The Unicode Standar ...
- C# \uxxx Unicode编码解码
/// <summary> /// Unicode编码 /// </summary> /// <param name="str"></pa ...
- Unicode 编码解码
1. Regex.Unescape(str);返回Unicode解码,非Unicode直接返回 /// <summary> /// 2.转为Unicode编码 /// ...
- Asp.Net \uxxx Unicode编码解码
/// <summary> /// Unicode编码 /// </summary> /// <param name="str"></pa ...
- python Unicode 编码解码
1 #将Unicode转换成普通的Python字符串:"编码(encode)" 2 unicodestring = u"Hello world" 3 utf8s ...
- Sql Server UniCode编码解码
); set @s = N'揶'; select UniCode(@s),nchar(UniCode(@s)); 在 SQL Server 中处理 Unicode 字串常数时,您必需在所有的 Unic ...
- C# 如何将字符串形式的” \\u1234 “ 为 “ \u1234” 的unicode编码解码为中文
using System.Text.RegularExpressions; decodedStr = Regex.Unescape(escapeUnicodeStr);
- PHP解码unicode编码中文字符代码示例
在抓取某网站数据,结果在数据包中发现了一串编码的数据:"......\u65b0\u6d6a\u5fae\u535a......", 这其实是中文被unicode编码后了的数据,想 ...
- PHP中对汉字进行UNICODE编码和解码的实现
<?php /** PHP中对汉字进行UNICODE编码和解码的实现 **/ class Helper_Tool{ //php中的unicode编码转中文 static function uni ...
随机推荐
- Codeforces 1097E. Egor and an RPG game
传送门 首先考虑怎么算 $f(n)$ (就是题目里面那个 $f(n)$) 发现可以构造一组序列大概长这样: ${1,3,2,6,5,4,10,9,8,7,15,14,13,12,11,...,n(n+ ...
- 怎样获取不同环境下的document对象
1. 在一般的网页文档中, 可通过: document 或 window.document 获取; 2. 在iframe框架中, 可通过iframe节点的属性: contentDocument 获取; ...
- python 使用三种常用的工具包处理图片
matplotlib,PIL(Pillow),Opencv三种常用的作图方式. 使用matplotlib画图,很棒,matplotlib 是python最著名的2D绘图库,它提供了一整套和matlab ...
- upxmake --- upx source compilation
upxmake --- upx source compilation 1. 下载upx所依赖的组件源码 zlib-1.2 http://www.zlib.net/zlib-1.2.11.tar.gz ...
- python3 多线程 采集 xpath
#!/usr/bin/python # -*- coding: UTF-8 -*- '''Thread3 多线程测试采集''' import threading,time,queue,Mongo_ut ...
- springcloud必知功能使用教程
springcloud Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线.负载均衡.断路 ...
- win10下 安装迅雷精简版
下载链接:https://files-cdn.cnblogs.com/files/del88/ThunderMini_1.5.3.288.zip 他妈的 今天安装迅雷精简版 在win10上 竟然报错, ...
- JS ES6
变量 let 块级作用域内有效 不能重复声明 不会预处理,不存在提升 var btns = document.getElementsByTagName('button'); for (let i = ...
- struts 漏洞
安装shop++ 安装成功 访问 http://127.0.0.1:8080 即网站首页 访问 http://127.0.0.1:8080/admin 即网站后台
- 【ASE高级软件工程】第一次结对作业
问题定义 具体规则见:讲义.大致规则如下: N个同学(N通常大于10),每人写一个0~100之间的有理数 (不包括0或100),交给裁判,裁判算出所有数字的平均值,然后乘以0.618(所谓黄金分割常数 ...