Question

First of all I have a couple hours experience with java, so if its a bit easy question sorry about that.

Now, I have a byte array with size 2 And I want to get corresponding integer value from it. I use little endian notation. Here is my code:

public static int convertInt(byte[] twoByte){
       //TODO
}

Is there anyone to help me ?

Was it helpful?

Solution

return (twoByte[0] & 0xff) + ((twoByte[1] & 0xff) << 8)

gives the correct result, interpreting the two bytes as unsigned number.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top