Java中byte數組與long數組相互轉化

來源:互聯網
上載者:User
Java中byte數組與long數組相互轉化
ByteAndLongArrayTest代碼如下:
  1. package com.array.test;
  2. import java.io.IOException;
  3. /**
  4.  * @ version 1.0
  5.  * @ author  wangwei
  6.  * @ created on  2008-10-17
  7.  */
  8. public class ByteAndLongArrayTest {
  9.     /**
  10.      * long數組轉化為byte數組
  11.      * 
  12.      * @param longArray
  13.      * @return
  14.      * @throws IOException
  15.      */
  16.     public static byte[] longToByte(long[] longArray)throws IOException{
  17.         byte[] byteArray=new byte[longArray.length*8];
  18.         for(int i=0;i<longArray.length;i++)
  19.         {
  20.             byteArray[0+8*i]=(byte)(longArray[i]>>56);
  21.             byteArray[1+8*i]=(byte)(longArray[i]>>48);
  22.             byteArray[2+8*i]=(byte)(longArray[i]>>40);
  23.             byteArray[3+8*i]=(byte)(longArray[i]>>32);
  24.             byteArray[4+8*i]=(byte)(longArray[i]>>24);
  25.             byteArray[5+8*i]=(byte)(longArray[i]>>16);
  26.             byteArray[6+8*i]=(byte)(longArray[i]>>8);
  27.             byteArray[7+8*i]=(byte)(longArray[i]>>0);
  28.         }
  29.         return byteArray;
  30.     }
  31.     
  32.     /**
  33.      * byte數組轉化為long數組
  34.      * 
  35.      * @param byteArray
  36.      * @return
  37.      * @throws IOException
  38.      */
  39.     public static long[] byteToLong(byte[] byteArray)throws IOException{
  40.         
  41.         long [] longArray=new long[byteArray.length/8];
  42.         for(int i=0;i<longArray.length;i++)
  43.         {
  44.             longArray[i]=(((long)byteArray[0+8*i]&0xff)<<56)
  45.                         |(((long)byteArray[1+8*i]&0xff)<<48)
  46.                         |(((long)byteArray[2+8*i]&0xff)<<40)
  47.                         |(((long)byteArray[3+8*i]&0xff)<<32)
  48.                         |(((long)byteArray[4+8*i]&0xff)<<24)
  49.                         |(((long)byteArray[5+8*i]&0xff)<<16)
  50.                         |(((long)byteArray[6+8*i]&0xff)<<8)
  51.                         |(((long)byteArray[7+8*i]&0xff)<<0);
  52.             
  53.         }
  54.         return longArray;
  55.     }
  56.     /**
  57.      * @param args
  58.      */
  59.     public static void main(String[] args) throws Exception{
  60.         // TODO Auto-generated method stub
  61.         long [] longArray={1,2,3,4,500000};
  62.         byte [] byteArray=longToByte(longArray);
  63.         for(int i=0;i<byteArray.length;i++)
  64.         {
  65.             System.out.print(byteArray[i]+" ");
  66.         }
  67.         System.out.println();
  68.         long [] longArray2=byteToLong(byteArray);
  69.         for(int i=0;i<longArray.length;i++)
  70.             System.out.print(longArray[i]+" ");
  71.     }
  72. }
相關文章

聯繫我們

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