Unicode转UTF-8(安卓android)
来源:互联网 发布:剑灵灵剑卡刀宏数据 编辑:程序博客网 时间:2024/06/10 14:50
欢迎访问我的技术博客Bigflower http://www.flowerfat.com
-----------------------------------
今天在使用一个【手机号码归属地api】的时候,返回值里中文是Unicode格式,样子是这样的:
"province":"\u9ed1\u9f99\u6c5f","city":"\u7261\u4e39\u6c5f"我们的目标是转换成可使用的UTF-8形式,我百度到了两种方法:
第一种方法很丑:
/** * 这个方法乍一看我尼玛太丑了,可是比下面的方法 好用! * * @param theString * @return */ 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') { // Read the xxxx 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(); }
第二种方法很好看:
/** * 这个方法,看着爽,效率低(时间长,耗内存) * * @param text * @return */ public static String unicode2Utf8(String text) { try { byte[] converttoBytes = text.getBytes("UTF-8"); String s2 = new String(converttoBytes, "UTF-8"); return s2; } catch (Exception e) { e.printStackTrace(); return e.getMessage(); } }
说下我的理由吧,写了一段测试方法:
private void test(){ String text = "\u5e7f\u5c9b\u4e4b\u604b.mp3"; long beginTime1 = System.currentTimeMillis() ; Log.i("方法一", "开始"); for (double i=0; i<25000;i++){ Util.decodeUnicode(text); } Log.i("方法一", "共耗时:"+(System.currentTimeMillis()-beginTime1)); long beginTime2 = System.currentTimeMillis() ; Log.i("方法二", "开始"); for (double i=0; i<25000;i++){ Util.unicode2Utf8(text); } Log.i("方法二", "共耗时:" + (System.currentTimeMillis() - beginTime2)); }
我们每种方法跑25000次,并且打印了两种方法的耗时,我们来看结果:
方法二不仅耗时长,而且进行了4次GC。结果不言而喻吧。 Way One Win !!
1 0
- Unicode转UTF-8(安卓android)
- Unicode转UTF-8(安卓android)
- UTF-8 转 Unicode
- UTF-8 转 Unicode
- Unicode转UTF-8
- unicode转utf-8
- Unicode转utf-8
- Unicode转UTF-8
- UTF-8 转 UNIcode
- php unicode转utf-8
- ios Unicode 转 UTF-8
- unicode 转 utf-8编码
- iOS unicode转utf-8
- 汉字utf-8转unicode
- Android http请求例子、Unicode转UTF-8 java代码
- android 基础 Unicode编码统一标准 utf-8
- Unicode UTF-8 UTF-16
- UNICODE,UTF-8,UTF-16
- git config 使用讲解
- Android编译系统分析
- (一) Laravel学习笔记之构造服务端API——数据填充//待完善V1.0版
- panic early exception 0d rip 10:ffffffff81038879 error 0 cr2 0
- Android界面美化 -- 自定义ListView分割线
- Unicode转UTF-8(安卓android)
- 2015年欢聚时代(YY)校园招聘Java笔试题目
- 适用于长文本的TextView
- Linux代码安全
- 适用于长文本的TextView
- 表格和div中文字溢出隐藏加省略号
- centos7 的/etc/rc.local不会开机执行
- Android 省电开发之 JobSchedule
- leetcode15 3Sum