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)