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

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

 

 

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