android Utils常用代码


一、Log日志写入SD卡、命令执行方法

二、String字符串十进制/十六进制相互转换




=======================================================================================================================
一、Log日志写入SD卡、命令执行方法
1
/* 2 * Log日志记录打印 3 */ 4 public static void write_sys_log(String strlog) { 5 try { 6 strlog = "\r\n" + strlog + "\n\r"; 7 String fileName="/sdcard/hlog.txt"; 8 9 File file = new File(fileName); 10 if( !file.exists()) { 11 file.createNewFile(); 12 } 13 FileWriter writer = new FileWriter(fileName, true); 14 writer.write(strlog); 15 writer.close(); 16 17 } catch(Exception e) { 18 } 19 } 20 21 /** 22 * 删除命令 23 */ 24 public static void commandExec(String cmd) { 25 26 try { 27 28 Runtime.getRuntime().exec(cmd); 29 30 } catch (IOException e) { 31 32 e.printStackTrace(); 33 } 34 }

 二、String字符串十进制/十六进制相互转换

 1 private static String hexString="0123456789ABCDEF";
 2 
 3 public static String strEncode10_16(String str10) {
 4         byte[] bytes=str10.getBytes(); 
 5         StringBuilder sb= new StringBuilder(bytes.length * 2);
 6         
 7         for(int i=0;i<bytes.length;i++) 
 8         { 
 9             sb.append(hexString.charAt((bytes[i]&0xf0)>>4)); 
10             sb.append(hexString.charAt((bytes[i]&0x0f)>>0)); 
11         } 
12         return sb.toString();     
13     }
14 
15 public String strDecode16_10(String str16) {
16         
17         ByteArrayOutputStream baos = new ByteArrayOutputStream(str16.length()/2);
18         
19         for(int i=0;i<str16.length();i+=2) 
20             baos.write((hexString.indexOf(str16.charAt(i)) << 4 | hexString.indexOf(str16.charAt(i+1)))); 
21             return new String(baos.toByteArray());     
22     }

 

posted @ 2016-04-22 16:06  海之涯2008  阅读(294)  评论(0编辑  收藏  举报