@Test
public void sin(){
int a = 11;
System.out.println(Arrays.toString(intToBytes(a)));
byte[] bs = intToBytes(a);
int i = bytesToInt(bs);
System.out.println(i);
}
/**
* int 4字节
* 序列化
* @param value
* @return
*/
public static byte[] intToBytes(int value){
byte[] byteArray = new byte[4];
//先写高位,把高位放到最后一个字节,也就是向右移动3个字节
byteArray[3] = (byte) ((value & 0xFF000000)>>3*8);
byteArray[2] = (byte) ((value & 0x00FF0000)>>2*8);
byteArray[1] = (byte) ((value & 0xFF00FF00)>>1*8);
byteArray[0] = (byte) (value & 0x000000FF);
return byteArray;
}
/**
* 反序列化
* @param byteArray
* @return
*/
public static int bytesToInt(byte[] byteArray){
return (byteArray[0] & 0xFF) |
((byteArray[1] << 8) & 0xFF00) |
((byteArray[2] << 16) & 0xFF0000) |
((byteArray[3] << 24) & 0xFF000000);
}
int类型序列化方法和反序列化方法
最新推荐文章于 2022-10-19 23:45:00 发布