package org.edujks.schoolapp.schoolbasedresearch.schoolsubject.action;

public class DecodeUtil {

    /**
* @declare native与ascii码互转
* @param args
* @author Lilin
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String a = chinaToUnicode("飞机f发多少adsfadsfadfad");
System.out.println(a); System.out.println(decodeUnicode(a)); } /**
* 把中文转成Unicode码
*
* @param str
* @return
*/
public static String chinaToUnicode(String str) {
String result = "";
for (int i = 0; i < str.length(); i++) {
int chr1 = (char) str.charAt(i);
if (chr1 >= 19968 && chr1 <= 171941) {// 汉字范围 \u4e00-\u9fa5 (中文)
result += "\\u" + Integer.toHexString(chr1);
} else {
result += str.charAt(i);
}
}
return result;
} /**
* 判断是否为中文字符
*
* @param c
* @return
*/
public static boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
return true;
}
return false;
} /***
* 转码
*
* @param theString
* @return
*
* @time 2012-6-13 下午5:54:34 TODO
*/
public static String decodeUnicode(String theString) {
char aChar;
int len = theString.length();
StringBuffer outBuffer = new StringBuffer(len);
for (int x = 0; x < len;) {
aChar = theString.charAt(x++);
if (aChar == '\\') {
aChar = theString.charAt(x++);
if (aChar == 'u') {
int value = 0;
for (int i = 0; i < 4; i++) {
aChar = theString.charAt(x++);
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.");
}
}
outBuffer.append((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';
outBuffer.append(aChar);
}
} else
outBuffer.append(aChar);
} return outBuffer.toString(); } }

native与ascii互转的更多相关文章

  1. python模块介绍- binascii:二进制和ASCII互转以及其他进制转换

    20.1 binascii:二进制和ASCII互转作用:二进制和ASCII互相转换. Python版本:1.5及以后版本 binascii模块包含很多在二进制和ASCII编码的二进制表示转换的方法.通 ...

  2. unicode ascii 互转 函数 C实现 MultiByteToWideChar/WideCharToMultiByte 详解

    void Ascii2UnicodeLen(char*src,int len,unsigned short*tar) { unsigned int word_cnt; word_cnt=MultiBy ...

  3. App技术框架

    一.App技术框架的类型 图1 三种App技术框架之间的关系 目前App的技术框架基本分为三种(图1): (1)Native App:互动型,iOS.Android.WP各一套,而且要维护历史版本,要 ...

  4. python剑指网络篇一

    #coding:utf-8 __author__ = 'similarface' #!/usr/bin/env python import socket #二进制和ASCII互转及其它进制转换 fro ...

  5. 计算机程序的思维逻辑 (64) - 常见文件类型处理: 属性文件/CSV/EXCEL/HTML/压缩文件

    对于处理文件,我们介绍了流的方式,57节介绍了字节流,58节介绍了字符流,同时,也介绍了比较底层的操作文件的方式,60节介绍了随机读写文件,61节介绍了内存映射文件,我们也介绍了对象的序列化/反序列化 ...

  6. 要让CLR挂掉的话……

    http://rednaxelafx.iteye.com/blog/460893 (Disclaimer:如果需要转载请先与我联系. 作者:RednaxelaFX -> rednaxelafx. ...

  7. IntelliJ IDEA(五) :Settings(中)

    上篇介绍了Settings中的Appearance & Behavior和Keymap,这篇继续,将介绍Editor,Plugins,Version Control. 一.Editor(编辑) ...

  8. Java编程的逻辑 (64) - 常见文件类型处理: 属性文件/CSV/EXCEL/HTML/压缩文件

    ​本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...

  9. Native2Ascii文件转换 -- 待完善

    摘自:https://www.oschina.net/code/snippet_87799_1612 Native2Ascii文件转换 -- 待完善 package com.xxx.xxx.Util; ...

随机推荐

  1. 利用JS实现图片的缓存

    web页面使用HTML的<img>元素来嵌入图片,和所有HTML元素一样,<img>元素也是可以通过脚本来操控的(设置元素的src属性,将其指向一个新的URL会导致浏览器载入并 ...

  2. Python核心编程——Chapter16

    好吧,在拜读完<Python网络编程基础>之后,回头再搞一搞16章的网络编程吧. Let‘s go! 16.4.修改书上示例的TCP和UDP客户端,使得服务器的名字不要在代码里写死,要允许 ...

  3. Javascript加速运动与减速运动

    加速运动,即一个物体运动时速度越来越快:减速运动,即一个物体运动时速度越来越慢.现在用Javascript来模拟这两个效果,原理就是用setInterval或setTimeout动态改变一个元素与另外 ...

  4. 加速计 & CoreMotion

    CHENYILONG Blog 加速计 & CoreMotion 加速计 & CoreMotion 技术博客http://www.cnblogs.com/ChenYilong/ 新浪微 ...

  5. <header><footer>引用

    示例:http://www.w3school.com.cn/html/html_layout.asp

  6. google浏览器测试时清理缓存、强制不用缓存刷新快捷键(常用、效率)

    Ctrl+Shift+Del  清除Google浏览器缓存的快捷键  Ctrl+Shift+R  重新加载当前网页而不使用缓存内容

  7. MYSQL导入数据出现The MySQL server is running with the --secure-file-priv

    MYSQL导入数据出现The MySQL server is running with the --secure-file-priv option so it cannot execute this ...

  8. 分模块开发创建service子模块——(八)

    1.右击父工程新建maven子模块

  9. spring事务详解(二)实例

    在Spring中,事务有两种实现方式: 编程式事务管理: 编程式事务管理使用底层源码可实现更细粒度的事务控制.spring推荐使用TransactionTemplate,典型的模板模式. 申明式事务管 ...

  10. readb(), readw(), readl(),writeb(), writew(), writel() 宏函数【转】

    转自:http://www.netfoucs.com/article/hustyangju/70429.html readb(), readw(), readl()函数功能:从内存映射的 I/O 空间 ...