Basic data type data in Java converted to byte[] array storage
1 Packagecom.wocqz.test;2 3 Public classTestbyte {4 5 /**6 * int turns into a byte array7 * */8 Public Static byte[] Int_byte (intID) {9 //Int is a 32-bit 4 byte creates a byte array of length 4Ten byte[] arr=New byte[4]; One AArr[0]= (byte) ((id>>0*8) &0xff); -Arr[1]= (byte) ((id>>1*8) &0xff); -Arr[2]= (byte) ((id>>2*8) &0xff); theArr[3]= (byte) ((id>>3*8) &0xff); - - returnarr; - } + - /** + * byte array reversed int A * */ at Public Static intByte_int (byte[] arr) { - - intI0= (int) ((ARR[0]&0XFF) <<0*8); - intI1= (int) ((ARR[1]&0XFF) <<1*8); - intI2= (int) ((ARR[2]&0XFF) <<2*8); - intI3= (int) ((ARR[3]&0XFF) <<3*8); in - returni0+i1+i2+i3; to } + - Public Static voidMain (string[] args) { the * byte[] Int_byte = Testbyte.int_byte (10); $ Panax Notoginseng for(byteb:int_byte) { -System.out.println ("------->" +b); the } + A the intByte_int =Testbyte.byte_int (int_byte); +System.out.println ("<----------" +byte_int); - $ } $}
Let's go first. &x0ff 16 binary to binary is 11111111
& Bitwise AND operator
Two operand median is 1 and result is 1
If two operations are bit one 1 another 0, the result is 0
That is, the arithmetic rule:0&0=0; 0&1=0; 1&0=0; 1&1=1;
Here the main role is to clear 0, take a number of the middle finger positioning
When we convert the ID into binary, we can move a few 8 bits to the X0FF on & and we can get the byte binary in the binary is converted into a binary stored in a byte array regardless of type of int type long
(Self-understanding--------------------------------------------------------------------------------------------------not to spray)
Basic data type data in Java converted to byte[] array storage