byte數組和Int之間的相互轉換

這裏簡單記錄下兩種轉換方式:

第一種:

1、int與byte[]之間的轉換(類似的byte short,long型)

  1. /**  
  2.     * 將int數值轉換爲佔四個字節的byte數組,本方法適用於(低位在前,高位在後)的順序。 和bytesToInt()配套使用 
  3.     * @param value  
  4.     *            要轉換的int值 
  5.     * @return byte數組 
  6.     */    
  7. public static byte[] intToBytes( int value )   
  8. {   
  9.     byte[] src = new byte[4];  
  10.     src[3] =  (byte) ((value>>24) & 0xFF);  
  11.     src[2] =  (byte) ((value>>16) & 0xFF);  
  12.     src[1] =  (byte) ((value>>8) & 0xFF);    
  13.     src[0] =  (byte) (value & 0xFF);                  
  14.     return src;   
  15. }  
  16.  /**  
  17.     * 將int數值轉換爲佔四個字節的byte數組,本方法適用於(高位在前,低位在後)的順序。  和bytesToInt2()配套使用 
  18.     */    
  19. public static byte[] intToBytes2(int value)   
  20. {   
  21.     byte[] src = new byte[4];  
  22.     src[0] = (byte) ((value>>24) & 0xFF);  
  23.     src[1] = (byte) ((value>>16)& 0xFF);  
  24.     src[2] = (byte) ((value>>8)&0xFF);    
  25.     src[3] = (byte) (value & 0xFF);       
  26.     return src;  
  27. }  

byte[]轉int

  1. /**  
  2.     * byte數組中取int數值,本方法適用於(低位在前,高位在後)的順序,和和intToBytes()配套使用 
  3.     *   
  4.     * @param src  
  5.     *            byte數組  
  6.     * @param offset  
  7.     *            從數組的第offset位開始  
  8.     * @return int數值  
  9.     */    
  10. public static int bytesToInt(byte[] src, int offset) {  
  11.     int value;    
  12.     value = (int) ((src[offset] & 0xFF)   
  13.             | ((src[offset+1] & 0xFF)<<8)   
  14.             | ((src[offset+2] & 0xFF)<<16)   
  15.             | ((src[offset+3] & 0xFF)<<24));  
  16.     return value;  
  17. }  
  18.   
  19.  /**  
  20.     * byte數組中取int數值,本方法適用於(低位在後,高位在前)的順序。和intToBytes2()配套使用 
  21.     */  
  22. public static int bytesToInt2(byte[] src, int offset) {  
  23.     int value;    
  24.     value = (int) ( ((src[offset] & 0xFF)<<24)  
  25.             |((src[offset+1] & 0xFF)<<16)  
  26.             |((src[offset+2] & 0xFF)<<8)  
  27.             |(src[offset+3] & 0xFF));  
  28.     return value;  
  29. }  

第二種:

1、int與byte[]之間的轉換(類似的byte short,long型)

  1.  /**  
  2.     * 將int數值轉換爲佔四個字節的byte數組,本方法適用於(低位在前,高位在後)的順序。  
  3.     * @param value  
  4.     *            要轉換的int值 
  5.     * @return byte數組 
  6.     */    
  7. public static byte[] intToBytes(int value)   
  8. {   
  9.     byte[] byte_src = new byte[4];  
  10.     byte_src[3] = (byte) ((value & 0xFF000000)>>24);  
  11.     byte_src[2] = (byte) ((value & 0x00FF0000)>>16);  
  12.     byte_src[1] = (byte) ((value & 0x0000FF00)>>8);    
  13.     byte_src[0] = (byte) ((value & 0x000000FF));          
  14.     return byte_src;  
  15. }  

byte[]轉int

  1.  /**  
  2.     * byte數組中取int數值,本方法適用於(低位在前,高位在後)的順序。 
  3.     *   
  4.     * @param ary  
  5.     *            byte數組  
  6.     * @param offset  
  7.     *            從數組的第offset位開始  
  8.     * @return int數值  
  9.     */    
  10. public static int bytesToInt(byte[] ary, int offset) {  
  11.     int value;    
  12.     value = (int) ((ary[offset]&0xFF)   
  13.             | ((ary[offset+1]<<8) & 0xFF00)  
  14.             | ((ary[offset+2]<<16)& 0xFF0000)   
  15.             | ((ary[offset+3]<<24) & 0xFF000000));  
  16.     return value;  
  17. }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章