public byte[] intToByte(int i) {
byte[] abyte0 = new byte[4];
abyte0[0] = (byte) (0xff & i);
abyte0[1] = (byte) ((0xff00 & i) >> 8);
abyte0[2] = (byte) ((0xff0000 & i) >> 16);
abyte0[3] = (byte) ((0xff000000 & i) >> 24);
return abyte0;
}
public static int bytesToInt(byte[] bytes) {
int addr = bytes[0] & 0xFF;
addr |= ((bytes[1] << 8) & 0xFF00);
addr |= ((bytes[2] << 16) & 0xFF0000);
addr |= ((bytes[3] << 24) & 0xFF000000);
return addr;
}
本文介绍了一种将整型数值转化为字节序列的方法,并提供了逆向转换的实现细节。通过具体的Java代码示例,展示了如何使用位操作来完成整数到字节数组的转换,以及如何从字节数组还原出原始的整数值。
967

被折叠的 条评论
为什么被折叠?



