public class demo01 {
public static byte[] int2Bytes(int id) {
byte[] arr = new byte[4];
for (int i = 0; i < arr.length; i++) {
arr[i] = (byte) (int) ((id >> i * 8) & 0xff);
}
return arr;
}
public static int byte2Int(byte[] arr) {
int result = 0;
for (int i = 0; i < arr.length; i++) {
int s = (int) ((arr[i] & 0xff) << i * 8);
result = result + s;
}
return result;
}
public static byte[] long2Bytes(int id){
byte[] arr = new byte[8];
for (int i = 0; i < arr.length; i++) {
arr[i] = (byte) (long) ((id >> i * 8) & 0xff);
}
return arr;
}
public static long byte2Long(byte[] arr){
long result = 0;
for (int i = 0; i < arr.length; i++) {
long s = (long) ((arr[i] & 0xff) << i * 8);
result = result + s;
}
return result;
}
public static void main(String[] args) {
byte[] arr = demo01.int2Bytes(8143);
for (int in : arr
) {
System.out.print(in + ",");
}
System.out.println();
System.out.println(demo01.byte2Int(arr));
byte[] arr2 = demo01.long2Bytes(8143);
for (int in : arr2
) {
System.out.print(in + ",");
}
System.out.println();
System.out.println(demo01.byte2Long(arr2));
}
}