Example usage for java.lang Short SIZE

List of usage examples for java.lang Short SIZE

Introduction

In this page you can find the example usage for java.lang Short SIZE.

Prototype

int SIZE

To view the source code for java.lang Short SIZE.

Click Source Link

Document

The number of bits used to represent a short value in two's complement binary form.

Usage

From source file:Main.java

public static void main(String args[]) {
    System.out.println("Size of a Short:" + Short.SIZE);
}

From source file:Main.java

public static Short byteArrayToShort(byte[] bytes) {
    if (bytes.length != (Short.SIZE / Byte.SIZE)) {
        throw new IllegalArgumentException("Incorrect array size to convert to a short");
    }//from w ww .j  a v  a 2  s  . c  o  m
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    buffer.order(ByteOrder.BIG_ENDIAN);
    return buffer.getShort();
}

From source file:Main.java

public static Short byteArrayToShortLE(byte[] bytes) {
    if (bytes.length != (Short.SIZE / Byte.SIZE)) {
        throw new IllegalArgumentException("Incorrect array size to convert to a short");
    }//from w  ww  .ja  v  a 2 s.com
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    return buffer.getShort();
}

From source file:Main.java

public static byte[] shortToByteArray(short value) {
    ByteBuffer buffer = ByteBuffer.allocate(Short.SIZE / Byte.SIZE);
    buffer.order(ByteOrder.BIG_ENDIAN);
    buffer.putShort(value);/*from   w  w w  .j  a  v a2s  .c om*/
    return buffer.array();
}

From source file:Main.java

public static byte[] shortToByteArrayLE(short value) {
    ByteBuffer buffer = ByteBuffer.allocate(Short.SIZE / Byte.SIZE);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.putShort(value);/*ww  w  .j  av  a  2 s .  c o m*/
    return buffer.array();
}

From source file:Main.java

/**
 * Merge short/*from  w w w. j a v a  2 s  .c om*/
 *
 * @param short1
 *            int1
 * @param short2
 *            int2
 *
 * @return int
 */
public static int mergeShort(int short1, int short2) {
    int tmp = 0;
    tmp |= short1 << Short.SIZE;
    tmp |= short2 & FULL_ONE_SHORT;
    return tmp;

}

From source file:Main.java

/**
 * Convert a one or two byte array of bytes to an unsigned two-byte integer.
 * //  w w w. j a  v a 2 s.  c o m
 * <p><b>NOTE:</b> This function mostly exists as an example of how to use 
 * twoBytesToUnsignedInt(). It is unlikely the use case it embodies
 * will occur often.</p>
 * 
 * @param ba
 * @return
 */
public static int byteArrayToUnsignedInt(byte[] byteArray) {
    if (byteArray.length > Integer.SIZE)
        throw new IllegalArgumentException("Array length must match one of the following types:\n Byte=="
                + Byte.SIZE + ", Short==" + Short.SIZE + ", Integer==" + Integer.SIZE);

    return (int) byteArrayToUnsignedLong(byteArray);
}

From source file:Main.java

/**
 * @param byteArray//w  w w  .j  ava2 s  .c  o m
 * @return
 */
public static long byteArrayToUnsignedLong(byte[] byteArray) {
    int length;
    long value = 0;
    if (byteArray.length == Byte.SIZE / Byte.SIZE) {
        length = Byte.SIZE / Byte.SIZE;
    } else if (byteArray.length == Short.SIZE / Byte.SIZE) {
        length = Short.SIZE / Byte.SIZE;
    } else if (byteArray.length == Integer.SIZE / Byte.SIZE) {
        length = Integer.SIZE / Byte.SIZE;
    } else if (byteArray.length == Long.SIZE / Byte.SIZE) {
        length = Long.SIZE / Byte.SIZE;
    } else
        throw new IllegalArgumentException("Array length must match one of the following types:\n Byte=="
                + Byte.SIZE + ", Short==" + Short.SIZE + ", Integer==" + Integer.SIZE + ", Long==" + Long.SIZE);

    for (int i = 0; i < length; i++) {
        value |= ((0xffL & byteArray[i]) << (8 * (length - i - 1)));
    }
    return value;
}

From source file:ByteString.java

public int getStoredLength() {
    return bytes.length + (Short.SIZE / Byte.SIZE);
}

From source file:com.bah.culvert.util.Bytes.java

/**
 * Convert a short to a byte []/*  w ww . j  a  v a  2s .com*/
 * @param i
 * @return
 */
public static byte[] toBytes(short i) {
    int bytes = Short.SIZE / 8;

    return ByteBuffer.allocate(bytes).putShort(i).array();
}