/**
* unicode编码转换为汉字
* @param unicodeStr 待转化的编码
* @return 返回转化后的汉子
*/
public static String UnicodeToCN(String unicodeStr) {
Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
Matcher matcher = pattern.matcher(unicodeStr);
char ch;
while (matcher.find()) {
//group
String group = matcher.group(2);
//ch:'李四'
ch = (char) Integer.parseInt(group, 16);
//group1
String group1 = matcher.group(1);
unicodeStr = unicodeStr.replace(group1, ch + "");
} return unicodeStr.replace("\\", "").trim();
}
/**
* 汉字转化为Unicode编码
* @param CN 待转化的中文
* @return 返回转化之后的unicode编码
*/
public static String CNToUnicode(String CN) { try {
StringBuffer out = new StringBuffer("");
//直接获取字符串的unicode二进制
byte[] bytes = CN.getBytes("unicode");
//然后将其byte转换成对应的16进制表示即可
for (int i = 0; i < bytes.length - 1; i += 2) {
out.append("\\u");
String str = Integer.toHexString(bytes[i + 1] & 0xff);
for (int j = str.length(); j < 2; j++) {
out.append("0");
}
String str1 = Integer.toHexString(bytes[i] & 0xff);
out.append(str1);
out.append(str);
}
return out.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}

测试

 public static void main(String[] args) {
String Unicodestr = "\\u674e\\u56db";
System.out.println("unicode为\\u674e\\u56db对应的中文是:"+Util.UnicodeToCN(Unicodestr));
String CNStr = "李四";
System.out.println("李四对应的Unicode编码是:"+Util.CNToUnicode(CNStr)); }

测试结果:

这里可能需要解释的是:\ufeff。\ufeff表示的是UTF-16(大端序)的编码方式。在显示的时候可以将\ufeff过滤掉

Unicode编码与中文互转的更多相关文章

  1. Jmeter 处理Unicode编码转为中文

    对于接口中返回报文,有的接口返回信息是Unicode编码,写断言的时候,要么就Response Assertion就写成Unicode编码的断言,如下图: 真心不觉得返回报文加密能有多安全,别人将报文 ...

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

    前几天,遇到一个问题,就是在浏览器地址栏传递中文时,出现乱码,考虑了一下,解决方式有很多,我还是采用了转换编码的方式,将中文转换为Unicode编码,然后再解码成中文,以下是实现的过程,非常简单! p ...

  3. JavaScript为unicode编码转换为中文

    代码laycode - v1.1 关于这样的数据转换为中文问题,常用的以下方法. 1. eval解析或new Function("'+ str +'")()  str = eval ...

  4. php unicode编码和字符串互转

    php字符串转Unicode编码, Unicode编码转php字符 百度了很多,都一样, 要么不对, 要不就是只是把字符串的汉字转Unicode 经过多次试验查找, 找到了如下方法, 注意:字符串编码 ...

  5. Jmeter查看结果树Unicode编码转中文方法

    本文为转载微信公众号文章,如作者发现后不愿意,请联系我进行删除 在jmeter工具的使用中,不管是测试接口还是调试性能时,查看结果树必不可少,然而在查看响应数据时,其中的中文经常以Unicode的编码 ...

  6. PHP解码unicode编码的中文字符

    问题背景:晚上在抓取某网站数据,结果在数据包中发现了这么一串编码的数据:"......\u65b0\u6d6a\u5fae\u535a......www.jinyuanbao.cn" ...

  7. JavaScript中unicode编码与String互转(三种方法)

    1.引言 JS本身就支持unicode转string功能,一共有三种方式和String单个字符转unicode编码. 2.方法 //unicode转String 1. eval("'&quo ...

  8. scrapy 爬虫返回json格式内容unicode编码转换为中文的问题解决

    最近在基于python3.6.5 的环境使用scrapy框架爬虫获取json数据,返回的数据是unicode格式的,在spider里面的parse接口中打印response.text出来如下: cla ...

  9. VS2015解决非Unicode编码包含中文字段无法编译的问题

    用VS2015打开并编译,定位到编译错误的文件(.cs而不是可视化编辑视图) 文件--高级保存选项--编码 改为 Unicode-代码页1200 这样不论用VS2015/2013/2012/2010 ...

随机推荐

  1. getCanonicalFile与getAbsoluteFile区别

    package test; import java.io.File; import java.io.IOException; public class TestFilePath { public st ...

  2. Tensorflow 与Caffe(转)

    TensorFlow TensorFlow 是相对高阶的机器学习库,用户可以方便地用它设计神经网络结构,而不必为了追求高效率的实现亲自写 C++或 CUDA 代码.它和 Theano 一样都支持自动求 ...

  3. nohup.out文件过大解决方法 定时任务清空

    0.1和2分别表示标准输入.标准输出和标准错误信息输出,可以用来指定需要重定向的标准输入或输出. 在一般使用时,默认的是标准输出,既1.当我们需要特殊用途时,可以使用其他标号.例如,将某个程序的错误信 ...

  4. SpringBoot系统列 2 - 配置文件,多环境配置(dev,qa,online)

    实现项目的多环境配置的方法有很多,比如通过在Pom.xml中配置profiles(最常见) 然后在Install项目打War包的时候,根据需求打不同环境的包,如图: 这种配置多环境的方法在SSM框架中 ...

  5. View - RemoteViews

    设计Android的工程师起名字还是挺规范的,而且一眼就知道是什么意思.RemoteViews,顾名思义,远程的View.Android为了能让进程A显示进程B的View,设计了这么一种View(其实 ...

  6. NuGet Packages are missing,This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.

    错误内容 This project references NuGet package(s) that are missing on this computer. Use NuGet Package R ...

  7. lua 中protobuf repeated 嵌套类 复合类型

    PB基础知识科普 syntax = "proto2"; package PB; message Item { required string name = ; } message ...

  8. ansible运维工具(二)

    ansible playbook(二) 运行palybook时 要使用ansible-playbook命令 palybook执行任务的顺序是,在第一个主机上完成第一个任务,然后在第二个主机上完成第一个 ...

  9. Serv-u 备份处理

    需求分析 从删库到跑路,大家听过很多,但如果没删备份的话,那只能说玩的不够彻底,不专业.系统管理员一个非常重要的职责就是定期备份,以便在系统出现故障的时候能够及时恢复.我们来看下如何备份Serv-u系 ...

  10. 11.12git部分和redis

    2018-11-12 09:54:58 这星期把luffycity看完,然后周末回学校 越努力,越幸运!永远不要高估自己! 具体可以参考  戳我啊!! 一些git常用命令: 目前已使用Git的四个命令 ...