将Unicode编码转换为汉字字符串

/// <summary>
/// 汉字转换为Unicode编码
/// </summary>
/// <param name="str">要编码的汉字字符串</param>
/// <returns>Unicode编码的的字符串</returns>
public static 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;
}
/// <summary>
/// 将Unicode编码转换为汉字字符串
/// </summary>
/// <param name="str">Unicode编码字符串</param>
/// <returns>汉字字符串</returns>
public static string ToGB2312(string str)
{
string r = "";
MatchCollection mc = Regex.Matches(str, @"\\u([\w]{2})([\w]{2})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
byte[] bts = new byte[2];
foreach (Match m in mc)
{
bts[0] = (byte)int.Parse(m.Groups[2].Value, NumberStyles.HexNumber);
bts[1] = (byte)int.Parse(m.Groups[1].Value, NumberStyles.HexNumber);
r += Encoding.Unicode.GetString(bts);
}
return r;
}

时间: 2024-08-07 21:24:53

将Unicode编码转换为汉字字符串的相关文章

python之分析decode、encode、unicode编码转换为汉字

decode()方法使用注册编码的编解码器的字符串进行解码.它默认为默认的字符串编码.decode函数可以将一个普通字符串转换为unicode对象.decode是将普通字符串按照参数中的编码格式进行解析,然后生成对应的unicode对象,比如在这里我们代码用的是utf-8,那么把一个字符串转换为unicode就是如下形式:s2='哈'.decode('utf-8′),s2就是一个存储了'哈'字的unicode对象,其实就和unicode('哈', 'utf-8′)以及u'哈'是相同的. 例: s

python 将unicode编码转换为汉字的几种方法

str = '\u4eac\u4e1c\u653e\u517b\u7684\u722c\u866b' 方法1 使用unicode_escape 解码 print str.decode('unicode_escape') print unicode(str, 'unicode_escape') 方法2:若为json 格式,使用json.loads 解码 print json.loads('"%s"' %str) 方法3:使用eval print eval('u"%s"

中文转换成Unicode编码 和 Unicode编码转换为中文

前几天,遇到一个问题,就是在浏览器地址栏传递中文时,出现乱码,考虑了一下,解决方式有很多,我还是采用了转换编码的方式,将中文转换为Unicode编码,然后再解码成中文,以下是实现的过程,非常简单! package cy.code; public class CyEncoder { private String zhStr; //中文字符串 private String unicode;//将中文字符串转换为Unicode编码 存储在这个属性上. public CyEncoder(String z

JavaScript为unicode编码转换为中文

代码laycode - v1.1 关于这样的数据转换为中文问题,常用的以下方法. 1. eval解析或new Function("'+ str +'")()  str = eval("'" + str + "'"); // "我是unicode编码" 1 代码laycode - v1.1 2. unescape 解析  str = unescape(str.replace(/\u/g, "%u")); //

***PHP各种编码的汉字字符串截取

虽然PHP有现成的截取字符串函数substr(),但是这个函数不能对汉字字符串进行截取,要实现这种效果还需要我们自己去编写相应的函数.汉字有多种编码,比如GB2312,UTF-8等,汉字字符串的截取需要区分这种汉字编码,下面是给出的几个解决方案. 截取GB2312中文字符串 <?php //截取中文字符串- function mysubstr($str, $start, $len) { $tmpstr = ""; $strlen = $start + $len; for($i =

JavaScript为unicode编码转换为中文(转)

var str = "\\u6211\\u662Funicode\\u7F16\\u7801"; 关于这样的数据转换为中文问题,常用的以下方法. 1. eval解析或new Function("'+ str +'")() str = eval("'" + str + "'"); // "我是unicode编码" 2. unescape 解析 str = unescape(str.replace(/\u/g,

unicode编码转汉字

public static void main(String[] args) throws UnsupportedEncodingException  { int y = 20892; decodeUnicode("U+"+Integer.toHexString(y)) } //中文转Unicode public static String gbEncoding(final String gbString) {   //gbString = "测试" char[] 

Unicode编码转换汉字

Uri.UnescapeDataString(string) #region Unicode转换汉字 Console.WriteLine(Uri.UnescapeDataString("\u7B2C01\u96C6")); Console.WriteLine(Uri.UnescapeDataString("\"err_code_des\":\"\u8BA2\u5355\u5DF2\u5168\u989D\u9000\u6B3E\"&qu

C# Unicode编码

/// <summary> /// 汉字转换为Unicode编码 /// </summary> /// <param name="str">要编码的汉字字符串</param> /// <returns>Unicode编码的的字符串</returns> public static string ToUnicode(string str) { byte[] bts = Encoding.Unicode.GetBytes