/** 數組
* int到byte[] 由高位到低位 spa
* @param i 須要轉換爲byte數組的整行值。it
* @return byte數組 */ io
public static byte[] intToByteArray(int i) { function
byte[] result = new byte[4]; class
result[0] = (byte)((i >> 24) & 0xFF); static
result[1] = (byte)((i >> 16) & 0xFF); word
result[2] = (byte)((i >> 8) & 0xFF);co
result[3] = (byte)(i & 0xFF); new
return result;
}
/**
* byte[]轉int
* @param bytes 須要轉換成int的數組
* @return int值
*/
public static int byteArrayToInt(byte[] bytes) {
int value=0;
for(int i = 0; i < 4; i++) {
int shift= (3-i) * 8;
value +=(bytes[i] & 0xFF) << shift;
}
return value;
}