Java 字符串,byte[],16进制的字符串互转 收藏

来源:互联网 发布:余额宝2017知乎 编辑:程序博客网 时间:2024/06/07 18:47


在调试的时候,如果要显示byte的值是否与预期一致,一般转换为16进制的字符串,或者使用base64转换后,然后显示出来。

 

view plaincopy to clipboardprint?
  1. /** 
  2.  * 字符串转换成十六进制字符串 
  3.  */  
  4.   
  5. public static String str2HexStr(String str) {  
  6.   
  7.     char[] chars = "0123456789ABCDEF".toCharArray();  
  8.     StringBuilder sb = new StringBuilder("");  
  9.     byte[] bs = str.getBytes();  
  10.     int bit;  
  11.     for (int i = 0; i < bs.length; i++) {  
  12.         bit = (bs[i] & 0x0f0) >> 4;  
  13.         sb.append(chars[bit]);  
  14.         bit = bs[i] & 0x0f;  
  15.         sb.append(chars[bit]);  
  16.     }  
  17.     return sb.toString();  
  18. }  
  19.   
  20. /** 
  21.  *  
  22.  * 十六进制转换字符串 
  23.  */  
  24.   
  25. public static String hexStr2Str(String hexStr) {  
  26.     String str = "0123456789ABCDEF";  
  27.     char[] hexs = hexStr.toCharArray();  
  28.     byte[] bytes = new byte[hexStr.length() / 2];  
  29.     int n;  
  30.     for (int i = 0; i < bytes.length; i++) {  
  31.         n = str.indexOf(hexs[2 * i]) * 16;  
  32.         n += str.indexOf(hexs[2 * i + 1]);  
  33.         bytes[i] = (byte) (n & 0xff);  
  34.     }  
  35.     return new String(bytes);  
  36. }  
  37.   
  38. /** 
  39.  * bytes转换成十六进制字符串 
  40.  */  
  41. public static String byte2HexStr(byte[] b) {  
  42.     String hs = "";  
  43.     String stmp = "";  
  44.     for (int n = 0; n < b.length; n++) {  
  45.         stmp = (Integer.toHexString(b[n] & 0XFF));  
  46.         if (stmp.length() == 1)  
  47.             hs = hs + "0" + stmp;  
  48.         else  
  49.             hs = hs + stmp;  
  50.         // if (n<b.length-1) hs=hs+":";  
  51.     }  
  52.     return hs.toUpperCase();  
  53. }  
  54.   
  55. private static byte uniteBytes(String src0, String src1) {  
  56.     byte b0 = Byte.decode("0x" + src0).byteValue();  
  57.     b0 = (byte) (b0 << 4);  
  58.     byte b1 = Byte.decode("0x" + src1).byteValue();  
  59.     byte ret = (byte) (b0 | b1);  
  60.     return ret;  
  61. }  
  62.   
  63. /** 
  64.  * bytes转换成十六进制字符串 
  65.  */  
  66. public static byte[] hexStr2Bytes(String src) {  
  67.     int m = 0, n = 0;  
  68.     int l = src.length() / 2;  
  69.     System.out.println(l);  
  70.     byte[] ret = new byte[l];  
  71.     for (int i = 0; i < l; i++) {  
  72.         m = i * 2 + 1;  
  73.         n = m + 1;  
  74.         ret[i] = uniteBytes(src.substring(i * 2, m), src.substring(m, n));  
  75.     }  
  76.     return ret;  
  77. }  
  78.   
  79. /** 
  80.  *String的字符串转换成unicode的String 
  81.  */  
  82. public static String str2Unicode(String strText) throws Exception {  
  83.     char c;  
  84.     String strRet = "";  
  85.     int intAsc;  
  86.     String strHex;  
  87.     for (int i = 0; i < strText.length(); i++) {  
  88.         c = strText.charAt(i);  
  89.         intAsc = (int) c;  
  90.         strHex = Integer.toHexString(intAsc);  
  91.         if (intAsc > 128) {  
  92.             strRet += "//u" + strHex;  
  93.         } else {  
  94.             // 低位在前面补00  
  95.             strRet += "//u00" + strHex;  
  96.         }  
  97.     }  
  98.     return strRet;  
  99. }  
  100.   
  101. /** 
  102.  *unicode的String转换成String的字符串 
  103.  */  
  104. public static String unicode2Str(String hex) {  
  105.     int t = hex.length() / 6;  
  106.     StringBuilder str = new StringBuilder();  
  107.     for (int i = 0; i < t; i++) {  
  108.         String s = hex.substring(i * 6, (i + 1) * 6);  
  109.         // 高位需要补上00再转  
  110.         String s1 = s.substring(24) + "00";  
  111.         // 低位直接转  
  112.         String s2 = s.substring(4);  
  113.         // 将16进制的string转为int  
  114.         int n = Integer.valueOf(s1, 16) + Integer.valueOf(s2, 16);  
  115.         // 将int转换为字符  
  116.         char[] chars = Character.toChars(n);  
  117.         str.append(new String(chars));  
  118.     }  
  119.     return str.toString();  

 

原创粉丝点击