Detailed analysis of Code Conversion between int and byte arrays in java
In java, you may encounter converting an int to an array of byte [] or an array of byte [] to an int. Next, let's think about how to implement it?
First, analyze the storage format of int in java memory.
As we all know, the int type occupies 4 bytes in the memory and is stored in the Complement Method (if you are not familiar with the source code, reverse code, and complement code, please refer to the relevant information ). Example:
The binary value in memory corresponding to integer-128 is
Integer 128 corresponds to the binary value in the memory
Then, consider how to put four bytes of the int type into the byte array.
There are two optional methods: first, the int low-byte data is placed at the low position of the byte array (Little-endian), Second, the int low-byte data is placed in the high position of the byte array (Big-endian). Both methods are supported. The actual usage is subject to the existing conventions. Here we useBig-endian. Take Integer type-128For example, the converted byte array is
After explaining the conversion ideas, see the Implementation Code as follows:
public static byte[] int2Bytes(int value, int len) { byte[] b = new byte[len]; for (int i = 0; i < len; i++) { b[len - i - 1] = (byte)((value >> 8 * i) & 0xff); } return b; }
You need to describe the next operator ">".> Is shifted to the right with a symbol, in the format of value> n. If n is greater than the binary number m of the value type (in the above example, the value is int type, then m is 32 bits), then shift n % m bits (% modulo operator) to the right ).
Note: careful readers may find that after the value shift operation, the phase must be consistent with the literal 0xff (Binary 00000000 00000000 00000000 11111111, the first three bytes of the value are set to 00000000, And the last byte remains unchanged. Do you need to perform this operation?
Actually, it is completely unnecessary.When int is forcibly converted to byte, the first three bytes are automatically discarded. Whether the values of the first three bytes are all 0 does not affect the result, therefore, it is totally redundant with operations. It has no effect and wastes a little bit of program execution efficiency.
After modification, the code is
b[len - i - 1] = (byte)((value >> 8 * i));
Next we will consider how to convert the byte array to an int.
Our idea is to extract a single byte from the byte array in sequence, restore each byte to the corresponding int through the and left shift operation, and finally sum up each int value, that is, the expected result. The text may not be clearly expressed. Take the byte array converted from integer-128 as an example to describe how to convert the byte array to integer-128.
The Code is as follows:
public static int bytes2Int(byte[] b, int start, int len) { int sum = 0; int end = start + len; for (int i = start; i < end; i++) { int n = ((int)b[i]) & 0xff; n <<= (--len) * 8; sum += n; } return sum; }
Note that the above Code forcibly converts B [I] to an int, which is actually redundant. During the & operation, the two operands are automatically converted to the int type. For details, refer to another blog titled java for byte, short, char, int, description of automatic type conversion during long operation.
Therefore, the simplified code is
int n = b[i] & 0xff;
Continue to think about whether the above & operations are necessary. Can you change them directly?
int n = b[i];
The answer is no. For a single byte type, the java memory occupies one byte and is also stored as a complement. For example, if the byte type variable-1 (the binary value is 1111 1111), the corresponding binary value is 1111 1111 1111 1111 1111 1111 1111 1111 after it is forcibly converted to the int type, it is not the 0000 0000 0000 0000 0000 0000 1111 1111 we need to phase with 0xff to set the first three bytes to 0.