如下是代码,请教一下两个方法体中的每句代码是什么意思,谢谢了。
public class ByteTrans {
public static byte[] intToBytes(int id) {
byte [] arr = new byte[4];
arr[0] = (byte)((int)(id >> 0 * 8) & 0xff);
arr[1] = (byte)((int)(id >> 1 * 8) & 0xff);
arr[2] = (byte)((int)(id >> 2 * 8) & 0xff);
arr[3] = (byte)((int)(id >> 3 * 8) & 0xff);
return arr;
}
public static int byteToInt(byte[] arr) {
int rs0 = (int)((arr[0] & 0xff) << 0 * 8);
int rs1 = (int)((arr[1] & 0xff) << 1 * 8);
int rs2 = (int)((arr[2] & 0xff) << 2 * 8);
int rs3 = (int)((arr[3] & 0xff) << 3 * 8);
return rs0 + rs1 + rs2 + rs3;
}
public static void main(String [] args) {
byte [] arr = intToBytes(8143);
for(byte b : arr) {
System.out.print(b + " ");
}
System.out.println("\n" + byteToInt(arr));
}
}
OuBa
相关分类