Java Utililty Methods Byte Array to Short

List of utility methods to do Byte Array to Short

Description

The list of methods to do Byte Array to Short are organized into topic(s).

Method

shortbytes2short(byte[] src)
bytesshort
return bytes2short(src, 0);
shortbytesToShort(byte A, byte B)
bytes To Short
short i = (short) (B & MASK_TO_BYTE);
i |= ((A & MASK_TO_BYTE) << 8);
return i;
shortbytesToShort(byte a, byte b, boolean swapBytes)
Concatenate two bytes to a short integer value.
if (swapBytes) {
    return (short) ((a & 0xff) + (b & 0xff) << 8);
} else {
    return (short) (((a & 0xff) << 8) + (b & 0xff));
shortbytesToShort(byte b1, byte b2)
bytes To Short
return (short) bytesToUshort(b1, b2);
shortbytesToShort(byte byte1, byte byte2)
bytes To Short
return (short) (0xffff & ((0xff & byte1) | ((0xff & byte2) << 8)));
shortbytesToShort(byte hiByte, byte loByte)
Reconstructs a short from its hi and low bytes.
int result = (0x000000FF & hiByte);
result = result << 8;
result |= (0x000000FF & loByte);
return (short) result;
shortbytesToShort(byte[] buf)
bytes To Short
return (short) (((0xff & buf[0]) << 8) | (0xff & buf[1]));
shortbytesToShort(byte[] buffer, int index)
This function converts the bytes in a byte array at the specified index to its corresponding short value.
int length = 2;
short integer = 0;
for (int i = 0; i < length; i++) {
    integer |= ((buffer[index + length - i - 1] & 0xFF) << (i * 8));
return integer;
shortbytesToShort(byte[] bytes)
16 bit.
return bytesToShort(bytes, 0);
shortbytesToShort(byte[] bytes)
Convert a byte array into a short takes the 4 smallest bits of every byte, you can give an array with less or more than 4 bytes put you have a risk of overflow or an unexpected result
short num = 0;
for (int i = 0; i < bytes.length; i++) {
    num += (bytes[i] & 0xF) << (i * 4);
return num;