Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to convert a byte array into an integer
How to convert a byte array into an integer
The following is a brief record of the two conversion methods:

The first kind:

1, the conversion between int and byte[] (similar to byte? Short and long types)

【java】? Opinion? Plain? Copy?

/**?

*? Converts an int value into a four-byte byte array. This method is suitable for sequence (low order first, high order last). ? Used with bytesToInt ()?

*? @param? Value?

* int value to be converted?

*? @ Return? Byte array?

*/

Public? Static electricity byte[]? intToBytes(? int? Value? )

{

byte[]? src? =? New? Byte [4]; ?

src[3]? =? (bytes)? ((value & gt& gt24)? & amp? 0x ff); ?

src[2]? =? (bytes)? ((value & gt& gt 16)? & amp? 0x ff); ?

src[ 1]? =? (bytes)? ((value & gt& gt8)? & amp? 0x ff);

src[0]? =? (bytes)? (value? & amp? 0x ff); ?

Return? src

}?

/**?

*? Converts an int value into a four-byte byte array. This method is suitable for sequence (high order first, low order later). ? Used with bytesToInt2 ()?

*/

Public? Static electricity byte[]? intToBytes2(int? Value)

{

byte[]? src? =? New? Byte [4]; ?

src[0]? =? (bytes)? ((value & gt& gt24)? & amp? 0x ff); ?

src[ 1]? =? (bytes)? ((value & gt>16); ? 0x ff); ?

src[2]? =? (bytes)? ((value & gt>8)&; 0x ff);

src[3]? =? (bytes)? (value? & amp? 0x ff);

Return? src?

}?

Byte[] to int

【java】? Opinion? Plain? Copy?

/**?

*? Accepts int values in byte arrays. This method is suitable for the order (low order comes first, high order comes last). Can I use it with intToBytes ()?

*

*? @param? src?

* byte array?

*? @param? Offset?

* Starting from the first offset bit of the array?

*? @ Return? Int value?

*/

Public? Static electricity int? bytesToInt(byte[]? src,? int? Offset)? {?

int? Value;

Value? =? (int)? ((src[offset]? & amp? 0xFF)

|? ((src[offset+ 1]? & amp? 0x ff)& lt; & lt8)

|? ((src[offset+2]? & amp? 0x ff)& lt; & lt 16)

|? ((src[offset+3]? & amp? 0x ff)& lt; & lt24)); ?

Return? Value; ?

}?

/**?

*? Int values are taken from byte arrays, and the appropriate order of this method is (low order last, high order first). Used with intToBytes2 ()?

*/?

Public? Static electricity int? bytesToInt2(byte[]? src,? int? Offset)? {?

int? Value;

Value? =? (int)? (? ((src[offset]? & amp? 0x ff)& lt; & lt24)?

|((src[offset+ 1]? & amp? 0x ff)& lt; & lt 16)?

|((src[offset+2]? & amp? 0x ff)& lt; & lt8)?

|(src[offset+3]? & amp? 0x ff)); ?

Return? Value; ?

}?

The second type: 1, int and byte[] (similar to byte

Short and long types)

【java】? Opinion? Plain? Copy?

/**?

*? Converts an int value into a four-byte byte array. This method is suitable for sequence (low order first, high order last). ?

*? @param? Value?

* int value to be converted?

*? @ Return? Byte array?

*/

Public? Static electricity byte[]? intToBytes(int? Value)

{

byte[]? byte_src? =? New? Byte [4]; ?

byte_src[3]? =? (bytes)? ((value? & amp? 0xFF000000)>& gt24); ?

byte_src[2]? =? (bytes)? ((value? & amp? 0x00FF0000)>& gt 16); ?

byte_src[ 1]? =? (bytes)? ((value? & amp? 0x0000FF00)>& gt8);

byte_src[0]? =? (bytes)? ((value? & amp? 0x 000000 ff)); ?

Return? byte _ src?

}?

Byte[] to int

【java】? Opinion? Plain? Copy?

/**?

*? Int values are taken from byte arrays, and the suitable order of this method is (low order first, high order last). ?

*

*? @param? ary?

* byte array?

*? @param? Offset?

* Starting from the first offset bit of the array?

*? @ Return? Int value?

*/

Public? Static electricity int? bytesToInt(byte[]? ary? int? Offset)? {?

int? Value;

Value? =? (int)? ((ary[offset]& amp; 0xFF)

|? ((ary[offset+ 1]& lt; & lt8)? & amp? 0xFF00)?

|? ((ary[offset+2]& lt; & lt 16); ? 0xFF0000)

|? ((ary[offset+3]& lt; & lt24)? & amp? 0x ff 000000)); ?

Return? Value; ?

}