Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How does java convert byte[4] array into int data?
How does java convert byte[4] array into int data?
The tools for converting a 4-byte array into an int value in java are as follows:

/**

* @param byte[]

* @return int

*/

public static int byteArrayToInt(byte[]b){

Byte[] a = new byte [4];

int i = a.length - 1,j = b . length- 1;

for(; I>= 0; I-, j-){// Copy data from the tail of b (i.e. the low order of int value).

if(j & gt; = 0)

a[I]= b[j];

other

a[I]= 0; //If b.length is less than 4, the upper bits will be filled with 0.

}

int v 0 =(a[0]& amp; 0x ff)& lt; & lt24; //& amp; 0xff converts byte values into int without any difference, so as to avoid the high-order sign bit being retained after Java automatic type promotion.

int v 1 =(a[ 1]& amp; 0x ff)& lt; & lt 16;

int v2 =(a[2]& amp; 0x ff)& lt; & lt8;

int v3 =(a[3]& amp; 0x ff);

Returns v0+v1+v2+v3;

}