天天看点

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