直接上代码

private static String decodeUnicode(String input) {
if (null == input)
return input;
int len = input.length();
StringBuilder output = new StringBuilder(len);
for (int x = 0; x < len; x++) {
char ch = input.charAt(x);
if (ch != '\\') {
output.append(ch);
} else {
x++;
if (x != len) {
ch = input.charAt(x);
if (ch == 'u') {
if (x + 5 > len) {
output.append(input.substring(x - 1));
x += 4;
} else {
String val = input.substring(x + 1, x + 5);
try {
output.append((char) Integer.parseInt(val, 16));
} catch (NumberFormatException e) {
output.append(input.substring(x - 1, x + 5));
}
x += 4;
}
} else
output.append(ch);
}
}
}
return output.toString();
} private static String encodeUnicode(String input) {
if (null == input)
return input;
int len = input.length();
StringBuilder output = new StringBuilder(len * 2);
for (int x = 0; x < len; x++) {
char ch = input.charAt(x);
if ((ch < ' ') || (ch > '~')) {
output.append("\\u");
String hex = Integer.toHexString(ch);
for (int i = 0; i < 4 - hex.length(); i++) {
output.append('0');
}
output.append(hex);
} else {
output.append(ch);
}
}
return output.toString();
}

注意stringbuilder和stringbuffer的区别

stringbuffer线程安全,stringbuilder线程不安全,二者功能完全一样。没有异步情况stringbuilder会快一些。

java字符串和unicode互转的更多相关文章

  1. Java - 字符串和Unicode互转 - 解析小米pm.min.js

    小米JS地址: http://p.www.xiaomi.com/zt/20130313/huodong/pm.min.js 上面这个JS是小米抢手机页面的代码.和抢手机有直接关联.. 虽然我3次都没抢 ...

  2. Java字符串与日期互转

    Java字符串与日期的相互转换 1.字符串转日期 字符串的格式与日期的格式一定要对应,并且字符串格式可以比日期格式多,但不能少,数字大小不自动计算日期.其中需要主要大小写 年yyyy 月MM 日dd ...

  3. java字符串大小写字母互改

    import java.util.Scanner; public class Test { /** * 测试数据 * * @param args */ public static void main( ...

  4. java 中文与unicode互转

    public class FontUtil { public static void main(String[] args) { System.out.println(chinaToUnicode(& ...

  5. java笔记 -- java字符串

    概念: Java字符串就是Unicode字符序列, Java没有内置的字符串类型, 而是在标准Java类库中提供了一个预定义类. 每个用双引号括起来的字符串都是String类的一个实例.String ...

  6. 浅谈Java字符串

    从概念上而言,Java字符串就是Unicode字符序列.由于Java没有内置的字符串类型,而是在标准Java类库中提供了一个预定义类String,每个用双引号的括起来的字符串都是String类的一个实 ...

  7. Java 字符串简介

    从概念上讲,Java 字符串就是 Unicode 字符序列.Java 没有内置的字符串类型,而是在标准 Java 类库中提供了一个预定义类,很自然地叫做 String.每个用双引号括起来的字符串都是 ...

  8. java对含有中文的字符串进行Unicode编码

    public class MyUtil { public static void main(String[] args) throws Exception { String s = "a中a ...

  9. Java中将字符串与unicode的相互转换工具类

    unicode编码规则 unicode码对每一个字符用4位16进制数表示.具体规则是:将一个字符(char)的高8位与低8位分别取出,转化为16进制数,如果转化的16进制数的长度不足2位,则在其后补0 ...

随机推荐

  1. css 层叠样式表

    css选择器 派生选择器 根据其元素在其上下文关系来定义样式 <style type="text/css">body{ font-size:12px; color:re ...

  2. ZOJ 3910 Market ZOJ Monthly, October 2015 - H

    Market Time Limit: 2 Seconds      Memory Limit: 65536 KB There's a fruit market in Byteland. The sal ...

  3. 分类 kNN

    #coding=utf-8 from numpy import * import operator from os import listdir import matplotlib import ma ...

  4. [leetCode][003] Intersection of Two Linked Lists

    [题目]: Write a program to find the node at which the intersection of two singly linked lists begins. ...

  5. EnableViewState=“false”不能乱用啊

    有时候页面源文件里有一段看上去像乱码的代码,这时候为了加快页面的加载速度,可以使用EnableViewState=“false”,这时候页面上的乱码就会消失了.但是,关于这个问题作者郁闷了好久,之前为 ...

  6. Spark中加载本地(或者hdfs)文件以及SparkContext实例的textFile使用

    默认是从hdfs读取文件,也可以指定sc.textFile("路径").在路径前面加上hdfs://表示从hdfs文件系统上读 本地文件读取 sc.textFile("路 ...

  7. .net 开发的奇淫巧计

    随机数 Random random = new Random(( int)DateTime .Now.Ticks & 0x0000FFFF); 如何让ASP.NET Web API显示完整的错 ...

  8. MongoDB高可用模式部署

    首先准备机器,我这里是在公司云平台创建了三台DB server,ip分别是10.199.144.84,10.199.144.89,10.199.144.90. 分别安装mongodb最新稳定版本: w ...

  9. 揪出Android流氓软件

    揪出Android流氓软件 http://www.icpcw.com/Smartphone/Android/Android/1471/147142_all.htm http://www.william ...

  10. 撑起大规模PHP网站的开源工具

    撑起大规模PHP网站的开源工具 百万级PHP站点Poppen.de的架构 在 2011年11月27日 那天写的     已经有 3957 次阅读了 感谢 参考或原文   服务器君一共花费了54.510 ...