- 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互转
最新推荐文章于 2024-04-24 16:48:15 发布