以下代码是将 int 转换为 Bytes 数组。我知道 inti
右移了 24、16、8 次,并与 0xFF 进行了与运算,但我不明白为什么要使用这些数字?
private static byte[] intToBytes(int i)
// split integer i into 4 byte array
{
// map the parts of the integer to a byte array
byte[] integerBs = new byte[4];
integerBs[0] = (byte) ((i >>> 24) & 0xFF);
integerBs[1] = (byte) ((i >>> 16) & 0xFF);
integerBs[2] = (byte) ((i >>> 8) & 0xFF);
integerBs[3] = (byte) (i & 0xFF);
// for (int j=0; j < integerBs.length; j++)
// System.out.println(" integerBs[ " + j + "]: " + integerBs[j]);
return integerBs;
} // end of intToBytes()