Byte[] The conversion between an array and an int

Source: Internet
Author: User

This post goes from: H T t p://blog.csdn.net/sunnyfans/article/details/8286906

1, int and byte[] conversion (similar to the byte Short,long type)

1  /** 2 * Converts an int value to a byte array of four bytes, this method applies to the order of (low in front, high in the rear). Compatible with Bytestoint ()3 * @param value4 * int value to convert5 * @return byte array6      */  7      Public Static byte[] Inttobytes (intvalue)8     { 9         byte[] src =New byte[4];Tensrc[3] =  (byte) ((value>> -) &0xFF); Onesrc[2] =  (byte) ((value>> -) &0xFF); Asrc[1] =  (byte) ((value>>8) &0xFF);  -src[0] =  (byte) (Value &0xFF);  -         returnsrc; the     } -      /**  - * Converts an int value to a byte array of four bytes, this method applies to the order (high in front, low in the back). Compatible with BytesToInt2 () -      */   +      Public Static byte[] IntToBytes2 (intvalue) -     {  +         byte[] src =New byte[4]; Asrc[0] = (byte) ((value>> -) &0xFF); atsrc[1] = (byte) ((value>> -) &0xFF); -src[2] = (byte) ((value>>8) &0xFF);  -src[3] = (byte) (Value &0xFF);  -         returnsrc; -}

byte[] Turn int

1  /** 2 * byte array to take an int value, this method applies to (low in front, high in the back) order, and Inttobytes () supporting the use of3      *  4 * @param src5 * byte array6 * @param offset7 * Starting from the offset bit of the array8 * @return int value9      */  Ten      Public Static intBytestoint (byte[] SRC,intoffset) { One         intvalue;  AValue = (int) ((Src[offset] &0xFF)  -| ((src[offset+1] &0xFF) <<8)  -| ((src[offset+2] &0xFF) << -)  the| ((src[offset+3] &0xFF) << -)); -         returnvalue; -     } -      +      /**  - * The byte array takes an int value, this method is applied to the order (low in the back, high in front). Compatible with IntToBytes2 () +      */ A      Public Static intBytesToInt2 (byte[] SRC,intoffset) { at         intvalue;  -Value = (int) ((Src[offset) &0xFF) << -) -| ((src[offset+1] &0xFF) << -) -| ((src[offset+2] &0xFF) <<8) -| (src[offset+3] &0xFF)); -         returnvalue; in}

The second type:

1, int and byte[] conversion (similar to the byte Short,long type)

1      /** 2 * Converts an int value to a byte array of four bytes, this method applies to the order of (low in front, high in the rear). 3 * @param value4 * int value to convert5 * @return byte array6      */  7      Public Static byte[] Inttobytes (intvalue)8     { 9         byte[] Byte_src =New byte[4];Tenbyte_src[3] = (byte) (Value &0xff000000) >> -); Onebyte_src[2] = (byte) (Value &0x00ff0000) >> -); Abyte_src[1] = (byte) (Value &0x0000FF00) >>8);  -byte_src[0] = (byte) (Value &0x000000ff));  -         returnbyte_src; the}

byte[] Turn int

1      /** 2 * The byte array takes an int value, this method is applied to the order (low in front, high in the rear). 3      *  4 * @param ary5 * byte array6 * @param offset7 * Starting from the offset bit of the array8 * @return int value9      */  Ten      Public Static intBytestoint (byte[] ary,intoffset) { One         intvalue;  AValue = (int) ((ary[offset]&0xFF)  -| ((ary[offset+1]<<8) &0xff00) -| ((ary[offset+2]<< -) &0xFF0000)  the| ((ary[offset+3]<< -) &0xff000000)); -         returnvalue; -}

Byte[] conversion between array and int (GO)

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.