天天看點

java中byte與int互轉

  • package com.yl.common.utils;  
  • /**
  •  * byte轉換工具
  •  * 
  •  * @author huangzp
  •  * @date 2015-6-09
  •  */
  • public

 class ByteUtil {  

  •     /**
  •      * 将iSource轉為長度為iArrayLen的byte數組,位元組數組的低位是整型的低位元組位
  •      * @param iSource
  •      * @param iArrayLen
  •      * @return
  •      */
  •     public

 static byte[] toByteArray(int iSource, int iArrayLen) {  

  •         byte

[] bLocalArr = new byte[iArrayLen];  

  •         for

 (int i = 0; (i < 4) && (i < iArrayLen); i++) {  

  •             bLocalArr[i] = (byte

) (iSource >> 8 * i & 0xFF);  

  •         }  
  •         return

 bLocalArr;  

  •     }  
  •      * 将byte數組bRefArr轉為一個整數,位元組數組的低位是整型的低位元組位
  •      * @param bRefArr

 static int toInt(byte[] bRefArr) {  

  •         int

 iOutcome = 0;  

 bLoop;  

 (int i = 0; i < 4; i++) {  

  •             bLoop = bRefArr[i];  
  •             iOutcome += (bLoop & 0xFF

) << (8 * i);  

 iOutcome;  

  • }  
  • package

 com.yl.common.utils;  

原文位址 http://techfoxbbs.com/blog-1-5.html