Python下进程与线程的原理及区别
2016-04-21 15:58
791 查看
下午同事说要一个Native2Ascii和Ascii2Native的Java实现,Google了半天发现有的是错误的实现,有的只有Native2Ascii没有Ascii2Native,有的又是其他语言实现的,干脆自己参考着写了一个。
package util; /** * native2ascii.exe Java code implementation. * * @author * @version 1.0 */ public class Native2AsciiUtils { /** * prefix of ascii string of native character */ private static String PREFIX = "\\u"; /** * Native to ascii string. It's same as execut native2ascii.exe. * * @param str * native string * @return ascii string */ public static String native2Ascii(String str) { char[] chars = str.toCharArray(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < chars.length; i++) { sb.append(char2Ascii(chars[i])); } return sb.toString(); } /** * Native character to ascii string. * * @param c * native character * @return ascii string */ private static String char2Ascii(char c) { if (c > 255) { StringBuilder sb = new StringBuilder(); sb.append(PREFIX); int code = (c >> 8); String tmp = Integer.toHexString(code); if (tmp.length() == 1) { sb.append("0"); } sb.append(tmp); code = (c & 0xFF); tmp = Integer.toHexString(code); if (tmp.length() == 1) { sb.append("0"); } sb.append(tmp); return sb.toString(); } else { return Character.toString(c); } } /** * Ascii to native string. It's same as execut native2ascii.exe -reverse. * * @param str * ascii string * @return native string */ public static String ascii2Native(String str) { StringBuilder sb = new StringBuilder(); int begin = 0; int index = str.indexOf(PREFIX); while (index != -1) { sb.append(str.substring(begin, index)); sb.append(ascii2Char(str.substring(index, index + 6))); begin = index + 6; index = str.indexOf(PREFIX, begin); } sb.append(str.substring(begin)); return sb.toString(); } /** * Ascii to native character. * * @param str * ascii string * @return native character */ private static char ascii2Char(String str) { if (str.length() != 6) { throw new IllegalArgumentException( "Ascii string of a native character must be 6 character."); } if (!PREFIX.equals(str.substring(0, 2))) { throw new IllegalArgumentException( "Ascii string of a native character must start with \"\\u\"."); } String tmp = str.substring(2, 4); int code = Integer.parseInt(tmp, 16) << 8; tmp = str.substring(4, 6); code += Integer.parseInt(tmp, 16); return (char) code; } }
相关文章推荐
- python中PIL安装简单教程
- 关于vs2010调用python中Py_Initialize函数报错的原因
- Python字符串部分源码分析
- 《Python核心编程 》笔记-第二章+第三章
- python 切片
- Python 插件安装
- selenium+webdriver+python 中警告框的处理方法
- python使用sqlite简单介绍
- Python reduce()求积
- 记录Python学习中的几个小问题
- Python3 字符编码问题
- w 4000 indow安装pip 并使用pip进行扩展包安装
- Python学习(21)python操作mysql数据库_操作
- 统计中分分词(CRF)
- python实现网站登陆功能
- python+selenium 关于div下拉框的处理
- 配置python的豆瓣source
- python操作xml文件
- python os.path.isdir权限问题小坑一个
- 初始化python类的实例时,私有变量的值与上一个实例的相同,问题定位