首页 > 编程语言 > 详细

java中byte与int互转

时间:2015-06-09 15:33:32      阅读:188      评论:0      收藏:0      [点我收藏+]

 

  • 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

 

  •      * @return

 

  •      */

  

  •     public

 static int toInt(byte[] bRefArr) {  

  •         int

 iOutcome = 0;  

  •         byte

 bLoop;  

  •   
  •         for

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

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

) << (8 * i);  

  •   
  •         }  
  •         return

 iOutcome;  

  •     }  
  •   
  • }  
  • 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

 

  •      * @return

 

  •      */

  

  •     public

 static int toInt(byte[] bRefArr) {  

  •         int

 iOutcome = 0;  

  •         byte

 bLoop;  

  •   
  •         for

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

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

) << (8 * i);  

  •   
  •         }  
  •         return

 iOutcome;  

  •     }  
  •   
  • }  ?

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

java中byte与int互转

原文:http://www.cnblogs.com/techfox/p/4563516.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!