【轉】Javabyte[]數組和十六進位String之間的轉換Util------包含案例和代碼

來源:互聯網
上載者:User

標籤:

原文網址:http://blog.csdn.net/caijunjun1006/article/details/11740223

Java中byte用二進位表示佔用8位,而我們知道16進位的每個字元需要用4位二進位位來表示(23 + 22 + 21 + 20 = 15),所以我們就可以把每個byte轉換成兩個相應的16進位字元,即把byte的高4位和低4位分別轉換成相應的16進位字元H和L,並組合起來得到byte轉換到16進位字串的結果new String(H) + new String(L)。即byte用十六進位表示只佔2位。

同理,相反的轉換也是將兩個16進位字元轉換成一個byte,原理同上。

根據以上原理,我們就可以將byte[] 數群組轉換為16進位字串了,當然也可以將16進位字串轉換為byte[]數組了。

 

下面是實現byte[]數組和十六進位字串之間轉化的工具類:

 

 

[java] view plaincopy 
  1. package text.com;  
  2.   
  3. public class BytesUtil {  
  4.     /** 
  5.     * Convert byte[] to hex string. 把位元組數組轉化為字串 
  6.     * 這裡我們可以將byte轉換成int,然後利用Integer.toHexString(int)來轉換成16進位字串。 
  7.     * @param src byte[] data 
  8.     * @return hex string 
  9.     */     
  10.    public static String bytesToHexString(byte[] src){  
  11.        StringBuilder stringBuilder = new StringBuilder("");  
  12.        if (src == null || src.length <= 0) {  
  13.            return null;  
  14.        }  
  15.        for (int i = 0; i < src.length; i++) {  
  16.            int v = src[i] & 0xFF;  
  17.            String hv = Integer.toHexString(v);  
  18.            if (hv.length() < 2) {  
  19.                stringBuilder.append(0);  
  20.            }  
  21.            stringBuilder.append(hv+" ");  
  22.        }  
  23.        return stringBuilder.toString();  
  24.    }  
  25.    /** 
  26.     * Convert hex string to byte[]   把為字串轉化為位元組數組 
  27.     * @param hexString the hex string 
  28.     * @return byte[] 
  29.     */  
  30.    public static byte[] hexStringToBytes(String hexString) {  
  31.        if (hexString == null || hexString.equals("")) {  
  32.            return null;  
  33.        }  
  34.        hexString = hexString.toUpperCase();  
  35.        int length = hexString.length() / 2;  
  36.        char[] hexChars = hexString.toCharArray();  
  37.        byte[] d = new byte[length];  
  38.        for (int i = 0; i < length; i++) {  
  39.            int pos = i * 2;  
  40.            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));  
  41.        }  
  42.        return d;  
  43.    }  
  44.    /** 
  45.     * Convert char to byte     
  46.     * @param c char 
  47.     * @return byte  
  48.     */  
  49.     private static byte charToByte(char c) {  
  50.        return (byte) "0123456789ABCDEF".indexOf(c);  
  51.    }  
  52. }  

下面是測試程式:

[java] view plaincopy 
  1. package text.com;  
  2.   
  3. public class Test {  
  4.     public static void main(String[] args) {  
  5.         byte b1 = 11;  
  6.         byte b2 = 21;  
  7.         byte b3 = 31;  
  8.         byte b4 = 41;  
  9.         byte b5 = 51;  
  10.         byte b6 = 61;  
  11.         byte b7 = 71;  
  12.         byte b8 = 81;  
  13.         byte[] bytes = new byte[] { b1, b2, b3, b4, b5, b6, b7, b8 };  
  14.         String hexString = "0b 15 1f 29 33 3d 47 51";  
  15.         String str = BytesUtil.bytesToHexString(bytes);  
  16.         System.out.println("str--->" + str);  
  17.         byte[] hexStringToBytes = BytesUtil.hexStringToBytes(hexString);  
  18.         for (int i = 0; i < hexStringToBytes.length; i++) {  
  19.             System.out.println("hexStringToBytes---->" + hexStringToBytes[i]);  
  20.         }  
  21.     }  
  22.   
  23. }  


控制台輸出:

 

str--->0b 15 1f 29 33 3d 47 51 
hexStringToBytes---->11
hexStringToBytes---->21
hexStringToBytes---->31
hexStringToBytes---->41
hexStringToBytes---->51
hexStringToBytes---->61
hexStringToBytes---->71
hexStringToBytes---->81

OK!

【轉】Javabyte[]數組和十六進位String之間的轉換Util------包含案例和代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.