1、int转换成4个byte数组
public static byte[] intToByteArray2(int a) { return new byte[] { (byte) ((a >> 24) & 0xFF), (byte) ((a >> 16) & 0xFF), (byte) ((a >> 8) & 0xFF), (byte) (a & 0xFF) }; }
2、4个byte数组转成int
public static int byteArrayToInt2(byte[] b) { return b[3] & 0xFF | (b[2] & 0xFF) << 8 | (b[1] & 0xFF) << 16 | (b[0] & 0xFF) << 24; }
3、long转成8个字节数组
public static byte[] longToBytes(long x){ ByteBuffer bb = ByteBuffer.allocate(8); bb.putLong(0, x); return bb.array(); }
public static long byteArrayTolong(byte[] b){ ByteBuffer bb = ByteBuffer.allocate(8); bb.put(b, 0, b.length); bb.flip(); return bb.getLong(); }