原文:http://topic.csdn.net/u/20070606/12/7cd05c3e-71da-42ff-b218-4fd0482f253b.html
package com.mapabc.sz_hbt.util;
/**
* <p>Title:整型与长度为4的字节数组的互换 </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2007-5-10</p>
* <p>Company: www.mapabc.com</p>
* @author luoyj
* @version 1.0
*/
public class ByteUtil {
/**
* 整型转换为4位字节数组
* @param intValue
* @return
*/
public static byte[] int2Byte(int intValue) {
byte[] b = new byte[4];
for (int i = 0; i < 4; i++) {
b[i] = (byte) (intValue >> 8 * (3 - i) & 0xFF);
//System.out.print(Integer.toBinaryString(b[i])+" ");
//System.out.print((b[i] & 0xFF) + " ");
}
return b;
}
/**
* 4位字节数组转换为整型
* @param b
* @return
*/
public static int byte2Int(byte[] b) {
int intValue = 0;
// int tempValue = 0xFF;
for (int i = 0; i < b.length; i++) {
intValue += (b[i] & 0xFF) << (8 * (3 - i));
// System.out.print(Integer.toBinaryString(intValue)+" ");
}
return intValue;
}
}