Question: I need to convert 2 byte array ( byte[2] ) to integer value in java. How can I do that?

Answer: 
In Java, Bytes are signed, which means a byte's value can be negative, and when that happens, @MattBall's original solution won't work.

For example, if the binary form of the bytes array are like this:

> 
> 1000 1101 1000 1101
> 
> 
> 

then myArray[0] is `1000 1101` and myArray[1] is `1000 1101`, the decimal value of byte `1000 1101` is `-115` instead of 141(= 2^7 + 2^3 + 2^2 + 2^0)

if we use 

`int result = (myArray[0] << 8) + myArray[1]`

the value would be `-16191` which is WRONG.

The reason why its wrong is that when we interpret a 2-byte array into integer, all the bytes are
unsigned, so when translating, we should map the signed bytes to unsigned integer:

`((myArray[0] & 0xff) << 8) + (myArray[1] & 0xff)`

the result is `36237`, use a calculator or ByteBuffer to check if its correct(I have done it, and yes, it's correct).
Well, each byte is an integer in the range -128..127, so you need a way to map a pair of integers to a single integer. There are many ways of doing that, depending on what you have encoded in the pair of bytes. The most common will be storing a 16-bit signed integer as a pair of bytes. Converting that back to an integer depends on whether you store it big-endian form:

```
(byte_array[0]<<8) + (byte_array[1] & 0xff)

```

or little endian:

```
(byte_array[1]<<8) + (byte_array[0] & 0xff)

```