Basic data type data in Java converted to byte[] array storage

Source: Internet
Author: User

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]&AMP;0XFF) <<0*8); -         intI1= (int) ((ARR[1]&AMP;0XFF) <<1*8); -         intI2= (int) ((ARR[2]&AMP;0XFF) <<2*8); -         intI3= (int) ((ARR[3]&AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.