Java Short Number Array Create toShorts(byte[] array, int offset, int length)

Here you can find the source of toShorts(byte[] array, int offset, int length)

Description

Returns a short array built from a byte array.

License

Open Source License

Declaration

public static short[] toShorts(byte[] array, int offset, int length) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from  w ww.  ja  va  2 s  .c o m*/
     * Returns a short array built from a byte array. Each 2 bytes form
     * a short with the first byte as the short's MSB (big-endian order).
     * <p>
     * If offset and length are omitted, the whole array is used.
     */
    public static short[] toShorts(byte[] array, int offset, int length) {
        short[] buf = new short[length / 2];
        int j = 0;

        for (int i = offset; i < offset + length - 1; i += 2)
            buf[j++] = (short) (((array[i] & 0xFF) << 8) | (array[i + 1] & 0xFF));

        return buf;
    }

    public static short[] toShorts(byte[] array) {
        return toShorts(array, 0, array.length);
    }
}

Related

  1. toShortArray(boolean[] array)
  2. toShorts(byte[] bytes)
  3. toShorts(byte[] readBuffer, int o, int l)
  4. toShorts(byte[] value, int offset, int num)
  5. toShorts(Short[] values)