今天,问以前的同事,他们写接口按什么编码,怎么看到有\u的一些看不懂的内容,一问,原来是信息隐藏,防止信息泄漏。

然后在网上查了Java如何把中文转换成unicode编码,转自:http://blog.csdn.net/sunmenggmail/article/details/27539023

 package mobi.chenwei.wing.util;

 public class CharacterSetToolkit {

     /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "天津";
System.out.println("Original:\t\t" + s); s = toEncodedUnicode(s, true);
System.out.println("to unicode:\t\t" + s); s = fromEncodedUnicode(s.toCharArray(), 0, s.length());
System.out.println("from unicode:\t" + s); }
private static final char[] hexDigit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
'B', 'C', 'D', 'E', 'F' }; private static char toHex(int nibble) {
return hexDigit[(nibble & 0xF)];
}
/**
* 将字符串编码成 Unicode 形式的字符串. 如 "黄" to "\u9EC4"
* Converts unicodes to encoded \\uxxxx and escapes
* special characters with a preceding slash
*
* @param theString
* 待转换成Unicode编码的字符串。
* @param escapeSpace
* 是否忽略空格,为true时在空格后面是否加个反斜杠。
* @return 返回转换后Unicode编码的字符串。
*/
public static String toEncodedUnicode(String theString, boolean escapeSpace) {
int len = theString.length();
int bufLen = len * 2;
if (bufLen < 0) {
bufLen = Integer.MAX_VALUE;
}
StringBuffer outBuffer = new StringBuffer(bufLen); for (int x = 0; x < len; x++) {
char aChar = theString.charAt(x);
// Handle common case first, selecting largest block that
// avoids the specials below
if ((aChar > 61) && (aChar < 127)) {
if (aChar == '\\') {
outBuffer.append('\\');
outBuffer.append('\\');
continue;
}
outBuffer.append(aChar);
continue;
} switch (aChar) {
case ' ':
if (x == 0 || escapeSpace) outBuffer.append('\\');
outBuffer.append(' ');
break;
case '\t':
outBuffer.append('\\');
outBuffer.append('t');
break;
case '\n':
outBuffer.append('\\');
outBuffer.append('n');
break;
case '\r':
outBuffer.append('\\');
outBuffer.append('r');
break;
case '\f':
outBuffer.append('\\');
outBuffer.append('f');
break;
case '=': // Fall through
case ':': // Fall through
case '#': // Fall through
case '!':
outBuffer.append('\\');
outBuffer.append(aChar);
break;
default:
if ((aChar < 0x0020) || (aChar > 0x007e)) {
// 每个unicode有16位,每四位对应的16进制从高位保存到低位
outBuffer.append('\\');
outBuffer.append('u');
outBuffer.append(toHex((aChar >> 12) & 0xF));
outBuffer.append(toHex((aChar >> 8) & 0xF));
outBuffer.append(toHex((aChar >> 4) & 0xF));
outBuffer.append(toHex(aChar & 0xF));
} else {
outBuffer.append(aChar);
}
}
}
return outBuffer.toString();
} /**
* 从 Unicode 形式的字符串转换成对应的编码的特殊字符串。 如 "\u9EC4" to "黄".
* Converts encoded \\uxxxx to unicode chars
* and changes special saved chars to their original forms
*
* @param in
* Unicode编码的字符数组。
* @param off
* 转换的起始偏移量。
* @param len
* 转换的字符长度。
* @param convtBuf
* 转换的缓存字符数组。
* @return 完成转换,返回编码前的特殊字符串。
*/
public static String fromEncodedUnicode(char[] in, int off, int len) {
char aChar;
char[] out = new char[len]; // 只短不长
int outLen = 0;
int end = off + len; while (off < end) {
aChar = in[off++];
if (aChar == '\\') {
aChar = in[off++];
if (aChar == 'u') {
// Read the xxxx
int value = 0;
for (int i = 0; i < 4; i++) {
aChar = in[off++];
switch (aChar) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
value = (value << 4) + aChar - '0';
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
value = (value << 4) + 10 + aChar - 'a';
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
value = (value << 4) + 10 + aChar - 'A';
break;
default:
throw new IllegalArgumentException("Malformed \\uxxxx encoding.");
}
}
out[outLen++] = (char) value;
} else {
if (aChar == 't') {
aChar = '\t';
} else if (aChar == 'r') {
aChar = '\r';
} else if (aChar == 'n') {
aChar = '\n';
} else if (aChar == 'f') {
aChar = '\f';
}
out[outLen++] = aChar;
}
} else {
out[outLen++] = (char) aChar;
}
}
return new String(out, 0, outLen);
}
}

运行结果:

Original: 天津
to unicode: \u5929\u6D25
from unicode: 天津

这时候,我们在写接口时,希望把中文进入这个方法中进行隐藏。

人生总是有许多事情是徒劳的,但是不要因为徒劳,而不去做这件事情,挖掘自己的潜力。

app开发历程————服务器端生成JSON格式数据,采用Unicode编码,隐藏中文的更多相关文章

  1. app开发历程————Android程序解析服务器端的JSON格式数据,显示在界面上

    上一篇文章写的是服务器端利用Servlet 返回JSON字符串,本文主要是利用android客户端访问服务器端链接,解析JSON格式数据,放到相应的位置上. 首先,android程序的布局文件main ...

  2. Java Servlet生成JSON格式数据并用jQuery显示

    1.Servlet通过json-lib生成JSON格式的数据 import java.io.IOException;import java.io.PrintWriter;import java.uti ...

  3. 如何使用fastJson来解析JSON格式数据和生成JSON格式数据

    由于项目用到了JSON格式的数据,在网上搜索到了阿里的fastjson比较好用,特此记录fastjson用法,以备以后查询之用. decode: 首先创建一个JSON解析类: public class ...

  4. Jquery DataTable AJAX跨域请求的解决方法及SSM框架下服务器端返回JSON格式数据的解决方法

    如题,用HBuilder开发APP,涉及到用AJAX跨域请求后台数据,刚接触,费了不少时间.幸得高手指点,得以解决. APP需要用TABLE来显示数据,因此采用了JQ 的DataTable.  在实现 ...

  5. 使用JSONObject类来生成json格式的数据

    JSONObject类不支持javabean转json 生成json格式数据的方式有: 1.使用JSONObject原生的来生成 2.使用map构建json格式的数据 3.使用javabean来构建j ...

  6. C#返回JSON格式数据

    又类的属性生成json格式数据 using System; using System.Collections.Generic; using System.Linq; using System.Web; ...

  7. iOS开发之JSON格式数据的生成与解析

    本文将从四个方面对IOS开发中JSON格式数据的生成与解析进行讲解: 一.JSON是什么? 二.我们为什么要用JSON格式的数据? 三.如何生成JSON格式的数据? 四.如何解析JSON格式的数据? ...

  8. 转载 -- iOS开发之JSON格式数据的生成与解析

    本文将从四个方面对IOS开发中JSON格式数据的生成与解析进行讲解: 一.JSON是什么? 二.我们为什么要用JSON格式的数据? 三.如何生成JSON格式的数据? 四.如何解析JSON格式的数据? ...

  9. ASP.NET Hashtable输出JSON格式数据

    最近在开发Windows8 Metro App,使用JavaScript和HTML开发环境.所以操作数据绑定都是使用JSON格式数据.后台使用的是ASP.NET,因为项目相对较小,所有后台没有使用数据 ...

随机推荐

  1. Windows下配置Nginx使之支持PHP(转)

    平台描述:Windows下,使用PHP套件 xampp,因为是测试玩,所以没在服务器 Linux 环境中配置. 1. 首先,将 nginx.conf 中的 PHP 配置注释去掉. 01 # pass ...

  2. mysql @变量和变量的区别及怎么判断记录唯一性

    DELIMITER// drop PROCEDURE if EXISTS test.express; create PROCEDURE test.express() BEGIN ) into @a f ...

  3. 使用 Java 配置进行 Spring bean 管理--转

    概述 众所周知,Spring 框架是控制反转 (IOC) 或依赖性注入 (DI) 模式的推动因素,而这种推动是通过基于容器的配置实现的.过去,Spring 允许开发人员使用基于 XML 的配置,通过利 ...

  4. [转] tomcat结合nginx使用小结

    相信很多人都听过nginx,这个小巧的东西慢慢地在吞食apache和IIS的份额.那究竟它有什么作用呢?可能很多人未必了解. 说到反向代理,可能很多人都听说,但具体什么是反向代理,很多人估计就不清楚了 ...

  5. [转] boost库的Singleton的实现以及static成员的初始化问题

    http://www.cnblogs.com/kex1n/archive/2011/04/05/2006194.html effectie c++的条款4中提到: (global对象,定义在names ...

  6. C# 读取XML文件示例

    有关XML文件编写规范,请参考:http://www.w3school.com.cn/xml/index.aspXML内容如下(文件名为:Information.xml):浏览器显示: <?xm ...

  7. DataTable数据与Excel表格的相互转换

    using Excel = Microsoft.Office.Interop.Excel; private static Excel.Application m_xlApp = null; /// & ...

  8. 转:android 自定义RadioButton样式

    http://gundumw100.iteye.com/blog/1146527  上面这种3选1的效果如何做呢?用代码写? 其实有更简单的办法,忘了RadioButton有什么特性了吗? 我就用Ra ...

  9. Android Studio中关于Project与Module

    在Android Studio中一个Project和Eclipse中的WorkSpace是相似的,而一个Module与Eclipse中的Project是相似的(大致可以这么的认为) 若在Android ...

  10. ajax传值

    $(function(){ $.ajax({ url:'order!seatnum.action', data:{ "entity.id":$("input[name=' ...