整理,以前很想弄清楚的字节转换。
1、int转成byte[]
方法:public static byte[] intToBytes(int id) {
byte[] arr = new byte[4];
arr[0] = (byte) ((id >> 0 * 8) & 0xFF);
arr[1] = (byte) ((id >> 1 * 8) & 0xFF);
arr[2] = (byte) ((id >> 2 * 8) & 0xFF);
arr[3] = (byte) ((id >> 3 * 8) & 0xFF);
return arr;
}
使用:byte[] arr=intToBytes(1234);
byte[]转成int
方法:public static int BytesToint(byte[] arr) {
int rs0=(int)((arr[0]&0xff)<<0*8);