# Java byte array type of byte[] interconverts to int type

- 2020-06-03 06:39:39
- OfStack

The code is as follows:

```
public class CommonUtils {
// High before, low after
public static byte[] int2bytes(int num){
byte[] result = new byte[4];
result[0] = (byte)((num >>> 24) & 0xff);// instructions 1
result[1] = (byte)((num >>> 16)& 0xff );
result[2] = (byte)((num >>> 8) & 0xff );
result[3] = (byte)((num >>> 0) & 0xff );
return result;
}
// High before, low after
public static int bytes2int(byte[] bytes){
int result = 0;
if(bytes.length == 4){
int a = (bytes[0] & 0xff) << 24;// instructions 2
int b = (bytes[1] & 0xff) << 16;
int c = (bytes[2] & 0xff) << 8;
int d = (bytes[3] & 0xff);
result = a | b | c | d;
}
return result;
}
public static void main(String[] args){
int a = -64;
System.out.println("-64="+Integer.toBinaryString(-64));
byte[] bytes = CommonUtils.int2bytes(a);
for(int i = 0 ; i<4 ; i++){
System.out.println(bytes[i]);
}
a = CommonUtils.bytes2int(bytes);
System.out.println(a);
}
}
```

The results are as follows:

```
-64=11111111111111111111111111000000
-1
-1
-1
-64
-64
```

Explanation 1:

-64 converted to 2 base code [10 million][00 million][00 million][00 million][00 million][01 million]

Changing the original code to complement is [11111111][11111111][11111111][11111111][111000000], which is the same as the console output. It can be seen that in java, base 2 is expressed in the form of complement

-64 > > > After 24 (unsigned right shift, high order complement 0), it becomes [00000000][00000000][00000000][00000000][00000000][0011111111]

Will step up the results & After 0xff, it is still [00000000][00000000][00000000][00000000][00000000][11111111], because the value of 0xff is [00000000][00000000][00000000][00000000][00111111], so & The aim of 0xff is to keep the lowest 8 bits unchanged and the rest 0 bits

Then, the strong result is converted to byte type, the low position is retained, and the high position is truncated to [11111111]. It can be seen that the previous 0xff is actually unnecessary, and no matter how high the high position is, it will be cut off eventually

So the result [0] to [11111111] = 1

And so on:

result [1] to [11111111] = 1

result [2] to [11111111] = 1

result [3] is [11000000] = - 64

2:

byte[0] is [11111111]. At first, byte[0] will be converted to int type (before displacement operation, byte type will be converted to int type; if positive, the high order complement will be 0; if negative, the high order supplement will be 1). The high order supplement will be [11111111][11111111][11111111][111111][111111][111111]

Will step up the results & After 0xff, it will become [00000000][00000000][00000000][00000000][11111111]

The result will then go up < < 24 (left displacement, low position complement 0), will become [11111111][00000000][00000000][00000000] [00000000] = a

Similarly, b, c and d were obtained

Finally a | b | c | d

[11111111][00000000][00000000][00000000] |

[00000000] [11111111] [00000000] [00000000] | because
<
<
16 before
&
0xff, so the maximum 8 bits of b are guaranteed to be 0

[00000000] [00000000] [11111111] [00000000] | because
<
<
Before eight
&
0xff, so the highest 16 bits of c are guaranteed to be 0

[00000000] [00000000] [00000000] [11000000] due & 0xff, so the maximum 24 of d is guaranteed to be 0

=[11111111][11111111][11111111][11000000] = -64

It can be seen that in order to ensure the conversion of byte to int, the complement does not affect the final result of a | b | c | d (set to 0), & 0xff is required

The conversion between short and byte[] and long and byte[] is similar

PS:

1, the int type takes up four bytes, while the byte type takes up only one byte

2, source code: the highest bit is the symbol bit, the rest bits are used to represent the value size

The original code of 2:00000010

Source code of -2:10000010

3. Reverse code: the reverse code of a positive number is the same as the original code; The inverse symbol bit of a negative number remains the same, while the other bits are reversed

The reverse code of 2:00000010

The inverse of -2:11111101

4. Complement: The complement of a positive number is the same as the original; The complement of a negative number is the reverse +1 of the negative number

Complement of 2:00000010

Complement of -2:11111110