Java Byte Array to Short byteToShort(byte[] byteData, boolean writeLittleEndian)

Here you can find the source of byteToShort(byte[] byteData, boolean writeLittleEndian)

Description

Function converts a byte array to a short array.

License

Open Source License

Parameter

Parameter Description
byteData The byte array.
writeLittleEndian If true data is handled as little endian, else as big endian

Return

The short array.

Declaration

public static short[] byteToShort(byte[] byteData, boolean writeLittleEndian) 

Method Source Code

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

public class Main {
    /**/*  w ww. j a  v a2s .co m*/
     * Function converts a byte array to a short array.
     * 
     * @param byteData
     *            The byte array.
     * @param writeLittleEndian
     *            If true data is handled as little endian, else as big endian
     * @return The short array.
     */
    public static short[] byteToShort(byte[] byteData, boolean writeLittleEndian) {
        short[] data = new short[byteData.length / 2];
        int size = data.length;
        byte lb, hb;
        if (writeLittleEndian) {
            for (int i = 0; i < size; i++) {
                lb = byteData[i * 2];
                hb = byteData[i * 2 + 1];
                data[i] = (short) (((short) hb << 8) | lb & 0xff);
            }
        } else {
            for (int i = 0; i < size; i++) {
                lb = byteData[i * 2];
                hb = byteData[i * 2 + 1];
                data[i] = (short) (((short) lb << 8) | hb & 0xff);
            }

        }
        return data;
    }
}

Related

  1. bytesToShortLittleEndian(final byte[] vals, final int from)
  2. bytesToShorts(byte byteBuffer[])
  3. bytesToShorts(byte[] byteData)
  4. bytesToShorts(byte[] src, int srcPos, short[] dest, int destPos, int length, boolean bigEndian)
  5. byteToShort(byte[] b)
  6. byteToShort(byte[] data, int length, boolean reduceStereo)