I usually use something like this:
static public int buildShort(byte high, byte low)
{
return ((0xFF & (int) high) * 256) + ((0xFF & (int) low));
}
Then you take the first two bytes of yours DatagramPacket:
int length = buildShort(packet.getData()[0], packet.getData()[1]);
Remember that I used length as int, because also the data type short(like every one) is signed in Java, so you need more space.
source
share