Java中int類型和tyte[]之間轉換及byte[]合并

來源:互聯網
上載者:User

標籤:

JAVA基於位移的 int類型和tyte[]之間轉換 [java] view plaincopy/**  * 基於位移的int轉化成byte[]  * @param int  number  * @return byte[]  */    public static byte[] intToByte(int number) {      byte[] abyte = new byte[4];      // "&" 與(AND),對兩個整型運算元中對應位執行布爾代數,兩個位都為1時輸出1,否則0。      abyte[0] = (byte) (0xff & number);      // ">>"右移位,若為正數則高位補0,若為負數則高位補1      abyte[1] = (byte) ((0xff00 & number) >> 8);      abyte[2] = (byte) ((0xff0000 & number) >> 16);      abyte[3] = (byte) ((0xff000000 & number) >> 24);      return abyte;  }    /**  *基於位移的 byte[]轉化成int  * @param byte[] bytes  * @return int  number  */    public static int bytesToInt(byte[] bytes) {      int number = bytes[0] & 0xFF;      // "|="按位或賦值。      number |= ((bytes[1] << 8) & 0xFF00);      number |= ((bytes[2] << 16) & 0xFF0000);      number |= ((bytes[3] << 24) & 0xFF000000);      return number;  }   JAVA 基於arraycopy合并兩個byte[] 數組 [java] view plaincopy/**  * 基於arraycopy合并兩個byte[] 數組  * @param byte[]  bytes1  * @param byte[]  bytes2  * @return   byte[]   bytes3   */      public   byte[]   combineTowBytes(byte[] bytes1,byte[] bytes2){              byte[] bytes3 = new byte[bytes1.length+bytes2.length];            System.arraycopy(bytes1,0,bytes3,0,bytes1.length);            System.arraycopy(bytes2,0,bytes3,bytes1.length,bytes2.length);            return bytes3 ;      }  

 

Java中int類型和tyte[]之間轉換及byte[]合并

聯繫我們

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