Here's a simple way to record the two conversions:
The first type:
1, int and byte[] conversion (similar to the byte Short,long type)
[Java]View PlainCopy
- /**
- * 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 ()
- * @param value
- * int value to convert
- * @return byte array
- */
- Public static byte[] Inttobytes ( int value)
- {
- byte[] src = new byte[4];
- src[3] = (byte) ((value>>) & 0xFF);
- src[2] = (byte) ((value>>) & 0xFF);
- src[1] = (byte) ((value>>8) & 0xFF);
- src[0] = (byte) (Value & 0xFF);
- return src;
- }
- /**
- * 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 (int value)
- {
- byte[] src = new byte[4];
- src[0] = (byte) ((value>>) & 0xFF);
- src[1] = (byte) ((value>>) & 0xFF);
- src[2] = (byte) ((value>>8) &0xFF);
- src[3] = (byte) (Value & 0xFF);
- return src;
- }
byte[] Turn int
[Java]View PlainCopy
- /**
- * Byte array to take an int value, this method applies to (low in front, high in the back) order, and Inttobytes () supporting the use of
- *
- * @param src
- * Byte array
- * @param offset
- * Starting from the offset bit of the array
- * @return int value
- */
- Public static int bytestoint (byte[] src, int offset) {
- int value;
- Value = (int) ((Src[offset] & 0xFF)
- | ((src[offset+1] & 0xFF) <<8)
- | ((src[offset+2] & 0xFF) <<
- | ((src[offset+3] & 0xFF) <<24));
- return value;
- }
- /**
- * The byte array takes an int value, this method is applied to the order (low in the back, high in front). Compatible with IntToBytes2 ()
- */
- Public static int BytesToInt2 (byte[] src, int offset) {
- int value;
- Value = (int) (((Src[offset] & 0xFF) <<
- | ((src[offset+1] & 0xFF) <<
- | ((src[offset+2] & 0xFF) <<8)
- | (src[offset+3] & 0xFF));
- return value;
- }
The second type:
1, int and byte[] conversion (similar to the byte Short,long type)
[Java]View PlainCopy
- /**
- * 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).
- * @param value
- * int value to convert
- * @return byte array
- */
- Public static byte[] Inttobytes (int value)
- {
- byte[] byte_src = new byte[4];
- byte_src[3] = (byte) ((Value & 0xff000000) >>24);
- byte_src[2] = (byte) ((Value & 0x00ff0000) >>16);
- byte_src[1] = (byte) ((Value & 0x0000FF00) >>8);
- byte_src[0] = (byte) ((Value & 0x000000ff));
- return BYTE_SRC;
- }
byte[] Turn int
[Java]View PlainCopy
- /**
- * The byte array takes an int value, this method is applied to the order (low in front, high in the rear).
- *
- * @param ary
- * Byte array
- * @param offset
- * Starting from the offset bit of the array
- * @return int value
- */
- Public static int bytestoint (byte[] ary, int offset) {
- int value;
- Value = (int) ((ary[offset]&0xFF)
- | ((ary[offset+1]<<8) & 0xff00)
- | ((ary[offset+2]<<) & 0xFF0000)
- | ((ary[offset+3]<<) & 0xff000000));
- return value;
- }
Byte[] The conversion between an array and an int