byte與進位及基本類型間的轉換

來源:互聯網
上載者:User

java的API源碼中隱含了一些對位元組處理的方法,以下複製黏貼或者修改後黏貼的代碼。

//把byte[]轉成int的方法,off指byte的開始位置    static int getInt(byte[] b, int off) {    return ((b[off + 3] & 0xFF) << 0) +           ((b[off + 2] & 0xFF) << 8) +           ((b[off + 1] & 0xFF) << 16) +           ((b[off + 0]) << 24);        }    //把int轉成byte[]的方法    static void putInt(byte[] b, int off, int val) {    b[off + 3] = (byte) (val >>> 0);    b[off + 2] = (byte) (val >>> 8);    b[off + 1] = (byte) (val >>> 16);    b[off + 0] = (byte) (val >>> 24);        }

上面這個是舉例,方法來源於Bits.class檔案。
以下是這個檔案的所有方法。

    /*     * Methods for unpacking primitive values from byte arrays starting at     * given offsets.     */    static boolean getBoolean(byte[] b, int off) {return b[off] != 0;    }        static char getChar(byte[] b, int off) {return (char) (((b[off + 1] & 0xFF) << 0) +        ((b[off + 0]) << 8));    }        static short getShort(byte[] b, int off) {return (short) (((b[off + 1] & 0xFF) << 0) + ((b[off + 0]) << 8));    }        static int getInt(byte[] b, int off) {return ((b[off + 3] & 0xFF) << 0) +       ((b[off + 2] & 0xFF) << 8) +       ((b[off + 1] & 0xFF) << 16) +       ((b[off + 0]) << 24);    }        static float getFloat(byte[] b, int off) {int i = ((b[off + 3] & 0xFF) << 0) +((b[off + 2] & 0xFF) << 8) +((b[off + 1] & 0xFF) << 16) +((b[off + 0]) << 24);return Float.intBitsToFloat(i);    }        static long getLong(byte[] b, int off) {return ((b[off + 7] & 0xFFL) << 0) +       ((b[off + 6] & 0xFFL) << 8) +       ((b[off + 5] & 0xFFL) << 16) +       ((b[off + 4] & 0xFFL) << 24) +       ((b[off + 3] & 0xFFL) << 32) +       ((b[off + 2] & 0xFFL) << 40) +       ((b[off + 1] & 0xFFL) << 48) +       (((long) b[off + 0]) << 56);    }    static double getDouble(byte[] b, int off) {long j = ((b[off + 7] & 0xFFL) << 0) + ((b[off + 6] & 0xFFL) << 8) + ((b[off + 5] & 0xFFL) << 16) + ((b[off + 4] & 0xFFL) << 24) + ((b[off + 3] & 0xFFL) << 32) + ((b[off + 2] & 0xFFL) << 40) + ((b[off + 1] & 0xFFL) << 48) + (((long) b[off + 0]) << 56);return Double.longBitsToDouble(j);    }        /*     * Methods for packing primitive values into byte arrays starting at given     * offsets.     */    static void putBoolean(byte[] b, int off, boolean val) {b[off] = (byte) (val ? 1 : 0);    }    static void putChar(byte[] b, int off, char val) {b[off + 1] = (byte) (val >>> 0);b[off + 0] = (byte) (val >>> 8);    }    static void putShort(byte[] b, int off, short val) {b[off + 1] = (byte) (val >>> 0);b[off + 0] = (byte) (val >>> 8);    }    static void putInt(byte[] b, int off, int val) {b[off + 3] = (byte) (val >>> 0);b[off + 2] = (byte) (val >>> 8);b[off + 1] = (byte) (val >>> 16);b[off + 0] = (byte) (val >>> 24);    }    static void putFloat(byte[] b, int off, float val) {int i = Float.floatToIntBits(val);b[off + 3] = (byte) (i >>> 0);b[off + 2] = (byte) (i >>> 8);b[off + 1] = (byte) (i >>> 16);b[off + 0] = (byte) (i >>> 24);    }    static void putLong(byte[] b, int off, long val) {b[off + 7] = (byte) (val >>> 0);b[off + 6] = (byte) (val >>> 8);b[off + 5] = (byte) (val >>> 16);b[off + 4] = (byte) (val >>> 24);b[off + 3] = (byte) (val >>> 32);b[off + 2] = (byte) (val >>> 40);b[off + 1] = (byte) (val >>> 48);b[off + 0] = (byte) (val >>> 56);    }    static void putDouble(byte[] b, int off, double val) {long j = Double.doubleToLongBits(val);b[off + 7] = (byte) (j >>> 0);b[off + 6] = (byte) (j >>> 8);b[off + 5] = (byte) (j >>> 16);b[off + 4] = (byte) (j >>> 24);b[off + 3] = (byte) (j >>> 32);b[off + 2] = (byte) (j >>> 40);b[off + 1] = (byte) (j >>> 48);b[off + 0] = (byte) (j >>> 56);    }

在DataInputStream裡也有部分這種方法。

    //修改自DataInputStream裡的方法    public static int toInt(byte[] b,int off) throws EOFException    {        int ch1 = b[off+0];        int ch2 = b[off+1];        int ch3 = b[off+2];        int ch4 = b[off+3];        if ((ch1 | ch2 | ch3 | ch4) < 0)            throw new EOFException();        return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));    }

不過這個類源碼中只有轉成基本類型的方法,仔細看和Bits.class檔案裡的方法差不多。
系統對基本類型轉成的byte是低位儲存高位。
比如short類型有16位,第一個byte儲存高8位,第二個位元組儲存低8位。

下面方法來源於網路,沒全部驗證其正確性。注意:其中有些轉byte的方法是把低位儲存在低位。

//long類型轉成byte數組 

 public static byte[] longToByte(long number) { 
       long temp = number; 
       byte[] b = new byte[8]; 
       for (int i = 0; i < b.length; i++) { 
           b[i] = new Long(temp & 0xff).byteValue();// 將最低位儲存在最低位 
           temp = temp >> 8; // 向右移8位 
       } 
       return b; 
   } 
   
   //byte數組轉成long 
   public static long byteToLong(byte[] b) { 
       long s = 0; 
       long s0 = b[0] & 0xff;// 最低位 
       long s1 = b[1] & 0xff; 
       long s2 = b[2] & 0xff; 
       long s3 = b[3] & 0xff; 
       long s4 = b[4] & 0xff;// 最低位 
       long s5 = b[5] & 0xff; 
       long s6 = b[6] & 0xff; 
       long s7 = b[7] & 0xff; 
 
       // s0不變 
       s1 <<= 8; 
       s2 <<= 16; 
       s3 <<= 24; 
       s4 <<= 8 * 4; 
       s5 <<= 8 * 5; 
       s6 <<= 8 * 6; 
       s7 <<= 8 * 7; 
       s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7; 
       return s; 
   } 
 
/** 
    * 注釋:int到位元組數組的轉換! 
    * 
    * @param number 
    * @return 
    */ 
   public static byte[] intToByte(int number) { 
       int temp = number; 
       byte[] b = new byte[4]; 
       for (int i = 0; i < b.length; i++) { 
           b[i] = new Integer(temp & 0xff).byteValue();//將最低位儲存在最低位 
           temp = temp >> 8; // 向右移8位 
       } 
       return b; 
   } 
 
   /** 
    * 注釋:位元組數組到int的轉換! 
    * 
    * @param b 
    * @return 
    */ 
   public static int byteToInt(byte[] b) { 
       int s = 0; 
       int s0 = b[0] & 0xff;// 最低位 
       int s1 = b[1] & 0xff; 
       int s2 = b[2] & 0xff; 
       int s3 = b[3] & 0xff; 
       s3 <<= 24; 
       s2 <<= 16; 
       s1 <<= 8; 
       s = s0 | s1 | s2 | s3; 
       return s; 
   } 
 
   /** 
    * 注釋:short到位元組數組的轉換! 
    * 
    * @param s 
    * @return 
    */ 
   public static byte[] shortToByte(short number) { 
       int temp = number; 
       byte[] b = new byte[2]; 
       for (int i = 0; i < b.length; i++) { 
           b[i] = new Integer(temp & 0xff).byteValue();// 將最低位儲存在最低位 
           temp = temp >> 8; // 向右移8位 
       } 
       return b; 
   } 
 
   /** 
    * 注釋:位元組數組到short的轉換! 
    * 
    * @param b 
    * @return 
    */ 
   public static short byteToShort(byte[] b) { 
       short s = 0; 
       short s0 = (short) (b[0] & 0xff);// 最低位 
       short s1 = (short) (b[1] & 0xff); 
       s1 <<= 8; 
       s = (short) (s0 | s1); 
       return s; 

   }

//把byte轉成16進位public static final String toHex(byte b) {//32位右移4位再把高28位清零,即求出來的是這個32位中的第四到第八位//  ....0000001111&b return ("" + "0123456789ABCDEF".charAt(0xf & b >> 4) + "0123456789ABCDEF".charAt(b & 0xf));}//把16進位的字串轉成byte[]public static byte[] hexStringToBytes(String hexString) {     if (hexString == null || hexString.equals("")) {         return null;     }     hexString = hexString.toUpperCase();     int length = hexString.length() / 2;     char[] hexChars = hexString.toCharArray();     byte[] d = new byte[length];     for (int i = 0; i < length; i++) {         int pos = i * 2;         d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));     }     return d;  } /**  * Convert char to byte  * @param c char  * @return byte  */   private static byte charToByte(char c) {      return (byte) "0123456789ABCDEF".indexOf(c);  //返回字元的十進位數並轉成2進位,1byte等於8位ASCII} //把byte[]轉成16進位顯示. public static String bytes2HexString(byte[] b) {   String ret = "";   for (int i = 0; i < b.length; i++) {    String hex = Integer.toHexString(b[ i ] & 0xFF);    if (hex.length() == 1) {     hex = '0' + hex;    }    ret += hex.toUpperCase();   }   return ret;  } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.