String类型 16进制编码解码

 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 static 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 @ 2015-07-15 14:35  海之涯2008  阅读(691)  评论(0)    收藏  举报