-
、/* int -> byte[] */
-
public static byte[] intToBytes(int num) {
-
byte[] b = new byte[4];
-
for (int i = 0; i < 4; i++) {
-
b[i] = (byte) (num >>> (24 - i * 8));
-
}
-
return b;
-
}
-
/* byte[]->int */
-
public final static int getInt(byte[] buf, boolean asc) {
-
if (buf == null) {
-
throw new IllegalArgumentException("byte array is null!");
-
}
-
if (buf.length > 4) {
-
throw new IllegalArgumentException("byte array size > 4 !");
-
}
-
int r = 0;
-
if (asc)
-
for (int i = buf.length - 1; i >= 0; i--) {
-
r <<= 8;
-
r |= (buf[i] & 0x000000ff);
-
}
-
else
-
for (int i = 0; i < buf.length; i++) {
-
r <<= 8;
-
r |= (buf[i] & 0x000000ff);
-
}
-
return r;
-
}
-