Java字節、十進制、十六進制、字符串之間的相互轉換

1. 字節轉10進制數組

直接使用(int)類型轉換。函數

    /*
     * 字節轉10進制
     */
    public static int byte2Int(byte b){
        int r = (int) b;
        return r;
    }

2. 10進制轉字節spa

直接使用(byte)類型轉換。code

    /*
     * 10進制轉字節
     */
    public static byte int2Byte(int i){
        byte r = (byte) i;
        return r;
    }

3. 字節數組轉16進制字符串blog

對每個字節,先和0xFF作與運算,而後使用Integer.toHexString()函數,若是結果只有1位,須要在前面加0。字符串

    /*
     * 字節數組轉16進制字符串
     */
    public static String bytes2HexString(byte[] b) {
        String r = "";
        
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            r += hex.toUpperCase();
        }
        
        return r;
    }

4. 16進制字符串轉字節數組get

這個比較複雜,每個16進制字符是4bit,一個字節是8bit,因此兩個16進制字符轉換成1個字節,對於第1個字符,轉換成byte之後左移4位,而後和第2個字符的byte作或運算,這樣就把兩個字符轉換爲1個字節。string

    /*
     * 字符轉換爲字節
     */
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
     }
    
    /*
     * 16進制字符串轉字節數組
     */
    public static byte[] hexString2Bytes(String hex) {
            
            if ((hex == null) || (hex.equals(""))){
                return null;
            }
            else if (hex.length()%2 != 0){
                return null;
            }
            else{                
                hex = hex.toUpperCase();
                int len = hex.length()/2;
                byte[] b = new byte[len];
                char[] hc = hex.toCharArray();
                for (int i=0; i<len; i++){
                    int p=2*i;
                    b[i] = (byte) (charToByte(hc[p]) << 4 | charToByte(hc[p+1]));
                }
              return b;
            }           
            
    }

5. 字節數組轉字符串it

直接使用new String()。io

    /*
     * 字節數組轉字符串
     */
    public static String bytes2String(byte[] b) throws Exception {
        String r = new String (b,"UTF-8");        
        return r;
    }

6. 字符串轉字節數組

直接使用getBytes()。

    /*
     * 字符串轉字節數組
     */
    public static byte[] string2Bytes(String s){
        byte[] r = s.getBytes();
        return r;
    }

7. 16進制字符串轉字符串

先轉換成byte[],再轉換成字符串。

    /*
     * 16進制字符串轉字符串
     */
    public static String hex2String(String hex) throws Exception{
        String r = bytes2String(hexString2Bytes(hex));        
        return r;
    }

8. 字符串轉16進制字符串

先轉換爲byte[],再轉換爲16進制字符串。

    /*
     * 字符串轉16進制字符串
     */
    public static String string2HexString(String s) throws Exception{
        String r = bytes2HexString(string2Bytes(s));        
        return r;
    }

main函數:

    public static void main(String[] args) throws Exception{        
        byte b1 = (byte) 45;
        System.out.println("1.字節轉10進制:" + byte2Int(b1));
        
        int i = 89;
        System.out.println("2.10進制轉字節:" + int2Byte(i));
        
        byte[] b2 = new byte[]{(byte)0xFF, (byte)0x5F, (byte)0x6, (byte)0x5A};
        System.out.println("3.字節數組轉16進制字符串:" + bytes2HexString(b2));
        
        String s1 = new String("1DA47C");
        System.out.println("4.16進制字符串轉字節數組:" + Arrays.toString(hexString2Bytes(s1)));
        
        System.out.println("5.字節數組轉字符串:" + bytes2String(b2));
        
        System.out.println("6.字符串轉字節數組:" + Arrays.toString(string2Bytes(s1)));
        
        System.out.println("7.16進制字符串轉字符串:" + hex2String(s1));
        
        String s2 = new String("Hello!");
        System.out.println("8.字符串轉16進制字符串:" + string2HexString(s2));
    }

運行結果:

1.字節轉10進制:45
2.10進制轉字節:89
3.字節數組轉16進制字符串:FF5F065A
4.16進制字符串轉字節數組:[29, -92, 124]
5.字節數組轉字符串:?_Z
6.字符串轉字節數組:[49, 68, 65, 52, 55, 67]
7.16進制字符串轉字符串:?|
8.字符串轉16進制字符串:48656C6C6F21
相關文章
相關標籤/搜索