java中的int与byte的转化

/** 数组

* 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;

}

相关文章
相关标签/搜索