您的位置:首页 > 移动开发 > Android开发

Android- Unicode编码 和 汉字转换( java实现 , javascript实现)

2015-10-08 18:25 609 查看

1.回顾

   好久没写文章了 , 不过最近在 弄自己的
个人博客 ,  还不错 , 使用 wordpress搭建而成的 . 还有就是 国庆假期 出去玩了, 该收收心了 .

   欢迎访问: http://www.labelnet.cn/

   今天在 自己的博客上实现 API接口的时候 , 遇到了 JSON API 插件 生成的 JSON数据汉字都是 unicode编码 , 使用起来安全性高 ,但是 需要编码为汉字.

2.Unicode 转 汉字

   2.1 java实现 

     (1) 第一种方式 

          具体的使用,自己可以封装下就行了.

<span style="font-size:18px;">package decodedemo;
public class UnicodeDecoder {

public static void main(String[] args) {
String s = "\u81f4\u6b49: \u672c\u6765\u8bf4\u662f\u8981\u5728\u5341\u4e00\u653e\u5047\u4e4b\u524d,\u5c06\u7f51\u9875\u7248\u7b80\u5386\u5236\u4f5c\u5b8c\u6210\u7684,\u5f88\u9057\u61be,\u6ca1\u6709\u505a\u5b8c. \u76f4\u5230\u4eca\u5929\u624d\u60f3\u8d77\u6765 ,\u8981\u5f00\u59cb\u5236\u4f5c\u4e86";
System.out.println(decode(s));
}

public static String decode(String in) {
try {
return decode(in.toCharArray());
} catch (Exception e) {
e.printStackTrace();
}
return in;
}

private static String decode(char[] in) throws Exception {
int off = 0;
char c;
char[] out = new char[in.length];
int outLen = 0;
while (off < in.length) {
c = in[off++];
if (c == '\\') {
if (in.length > off) { // 是否有下一个字符
c = in[off++]; // 取出下一个字符
} else {
out[outLen++] = '\\'; // 末字符为'\',返回
break;
}
if (c == 'u') { // 如果是"\\u"
int value = 0;
if (in.length > off + 4) { // 判断"\\u"后边是否有四个字符
boolean isUnicode = true;
for (int i = 0; i < 4; i++) { // 遍历四个字符
c = in[off++];
switch (c) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
value = (value << 4) + c - '0';
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
value = (value << 4) + 10 + c - 'a';
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
value = (value << 4) + 10 + c - 'A';
break;
default:
isUnicode = false; // 判断是否为unicode码
}
}
if (isUnicode) { // 是unicode码转换为字符
out[outLen++] = (char) value;
} else { // 不是unicode码把"\\uXXXX"填入返回值
off = off - 4;
out[outLen++] = '\\';
out[outLen++] = 'u';
out[outLen++] = in[off++];
}
} else { // 不够四个字符则把"\\u"放入返回结果并继续
out[outLen++] = '\\';
out[outLen++] = 'u';
continue;
}
} else {
switch (c) { // 判断"\\"后边是否接特殊字符,回车,tab一类的
case 't':
c = '\t';
out[outLen++] = c;
break;
case 'r':
c = '\r';
out[outLen++] = c;
break;
case 'n':
c = '\n';
out[outLen++] = c;
break;
case 'f':
c = '\f';
out[outLen++] = c;
break;
default:
out[outLen++] = '\\';
out[outLen++] = c;
break;
}
}
} else {
out[outLen++] = (char) c;
}
}
return new String(out, 0, outLen);
}
}
</span>
       

       (2)第二种方式

<span style="font-size:18px;">public static String decodeUnicode(final String dataStr) {
int start = 0;
int end = 0;
final StringBuffer buffer = new StringBuffer();
while (start > -1) {
end = dataStr.indexOf("\\u", start + 2);
String charStr = "";
if (end == -1) {
charStr = dataStr.substring(start + 2, dataStr.length());
} else {
charStr = dataStr.substring(start + 2, end);
}
char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。
buffer.append(new Character(letter).toString());
start = end;
}
return buffer.toString();
}</span>

   2.2 javascript 实现

<span style="font-size:18px;">
var uncode="\u6211\u53eb\u539f\u660e\u5353";
console.info("汉字:"+unescape(uncode.replace(/\u/g, "%u")));</span>

3. 汉字转 unicode编码

  3.1 java实现

<span style="font-size:18px;"> public static String gbEncoding(final String gbString) {
char[] utfBytes = gbString.toCharArray();
String unicodeBytes = "";
for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) {
String hexB = Integer.toHexString(utfBytes[byteIndex]);
if (hexB.length() <= 2) {
hexB = "00" + hexB;
}
unicodeBytes = unicodeBytes + "\\u" + hexB;
}
System.out.println("unicodeBytes is: " + unicodeBytes);
return unicodeBytes;
}</span>

  3.2 javascript实现

<span style="font-size:18px;">
function tounicode(data)
{
if(data == '') return '请输入汉字';
var str ='';
for(var i=0;i<data.length;i++)
{
str+="\\u"+parseInt(data[i].charCodeAt(0),10).toString(16);
}
return str;
}</span>

4.总结 

  具体的使用时,封装为工具类 ,方便开发.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息