Java Byte Array to Short bytesToShort(byte[] bytes)

Here you can find the source of bytesToShort(byte[] bytes)

Description

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

License

Open Source License

Parameter

Parameter Description
bytes the bytes which to convert

Return

the short created

Declaration

public static short bytesToShort(byte[] bytes) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**// ww  w  . j a v a2 s  .c om
     * 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
     * @param bytes the bytes which to convert
     *
     * @return the short created
     */
    public static short bytesToShort(byte[] bytes) {
        short num = 0;
        for (int i = 0; i < bytes.length; i++) {
            num += (bytes[i] & 0xF) << (i * 4);
        }
        return num;
    }
}

Related

  1. bytesToShort(byte byte1, byte byte2)
  2. bytesToShort(byte hiByte, byte loByte)
  3. bytesToShort(byte[] buf)
  4. bytesToShort(byte[] buffer, int index)
  5. bytesToShort(byte[] bytes)
  6. bytesToShort(byte[] bytes)
  7. bytesToShort(byte[] bytes, boolean netOrder)
  8. bytesToShort(byte[] bytes, int off)
  9. bytesToShort(byte[] bytes, int off, boolean bigEndian)