Android Byte Array to Short Convert bytesToShort(byte[] bytes)

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

Description

Converts a byte[] of unsigned bytes in big-endian order to a short.

License

Open Source License

Parameter

Parameter Description
bytes - A byte[] containing the bytes to convert.

Return

A short containing the equivalent signed value of the given bytes.

Declaration

public static short bytesToShort(byte[] bytes) 

Method Source Code

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

public class Main {
    /**/*  ww w  . ja  v a 2s  .co  m*/
     * Converts a byte[] of unsigned bytes in big-endian order to a short. 
     * 
     * @param bytes - A byte[] containing the bytes to convert.
     * 
     * @return A short containing the equivalent signed value of the given bytes.
     */
    public static short bytesToShort(byte[] bytes) {
        short s = 0;
        s |= (bytes[0] & 0xFF) << 8;
        s |= (bytes[1] & 0xFF);
        return s;
    }
}

Related

  1. bytesLE2sshorts(byte[] b)
  2. bytesLE2ushort(byte[] b, int off)
  3. bytesLE2ushorts(byte[] b)
  4. bytesToShort(byte[] b)
  5. bytesToShort(byte[] b)
  6. getShort(byte[] b, int index)
  7. getShort(byte[] b, int index)
  8. getShort(byte[] buf, boolean bigEndian)
  9. getShort(byte[] buf, int pos, boolean bigEndian)