Example usage for java.lang Byte SIZE

List of usage examples for java.lang Byte SIZE

Introduction

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

Prototype

int SIZE

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

Click Source Link

Document

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

Usage

From source file:Main.java

public static int byteArrayToIntLE(byte[] bytes) throws IllegalArgumentException {
    if (bytes.length != (Integer.SIZE / Byte.SIZE)) {
        throw new IllegalArgumentException("Incorrect array size to convert to an int");
    }/*from  w  w w  .j av a  2 s .  c o m*/
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    return buffer.getInt();
}

From source file:Main.java

public static short bytesToShort(byte[] bytes) {
    return (short) ((bytes[ONE] & MAX_BYTE) | (bytes[ZERO] & MAX_BYTE) << Byte.SIZE);
}

From source file:Main.java

public static String byteToBit(byte[] bytes) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < Byte.SIZE * bytes.length; i++)
        sb.append((bytes[i / Byte.SIZE] << i % Byte.SIZE & 0x80) == 0 ? '0' : '1');
    return sb.toString();
}

From source file:Main.java

public static short bytesToShort(byte[] bytes, int offset) {
    return (short) ((bytes[offset + ONE] & MAX_BYTE) | (bytes[offset] & MAX_BYTE) << Byte.SIZE);
}

From source file:Main.java

/**
 * Convert a one or two byte array of bytes to an unsigned two-byte integer.
 * // ww w. ja  v  a2 s.  com
 * <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

/**
 * 7 => 00000111/*from  w w  w .  j  a va2s .c o  m*/
 * 
 * @param src
 * @return
 */
public static String getBinaryString(byte src) {
    String binaryString = Integer.toBinaryString(src);
    if (binaryString.length() > Byte.SIZE) {
        binaryString = binaryString.substring(binaryString.length() - Byte.SIZE);
    } else {
        String temp = "";
        for (int i = 0; i < Byte.SIZE - binaryString.length(); i++) {
            temp += "0";
        }
        binaryString = temp + binaryString;
    }
    return binaryString;
}

From source file:Main.java

/**
 * @param byteArray//from ww  w  . j a  va2  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:Main.java

/**
 * Calculates the integer at the position <code>offset</code> in the buffer
 * @param buffer the offset of the integer
 * @param offset the buffer holding the integer.
 *
 * @return the calculated integer/*  w w w.  j  a  v a 2s  .com*/
 */
public static int intFromBuffer(byte[] buffer, int offset) {
    int result = 0;
    for (int i = 0; i < Integer.SIZE / Byte.SIZE; i++) {
        result <<= Byte.SIZE;
        result |= (buffer[offset + i] & 0xFF);
    }
    return result;
}

From source file:Main.java

/**
 * Takes the 8 bytes and converts them to an integer.
 *
 * @param buffer offset + 8 bytes containing the long
 * @param offset the index where the long start in the buffer
 * @return the long from the buffer.//from   w  ww. j a  va2 s .  c  om
 */
public static long longFromBuffer(byte[] buffer, int offset) {
    long result = 0;
    for (int i = 0; i < Long.SIZE / Byte.SIZE; i++) {
        result <<= Byte.SIZE;
        result |= (buffer[offset + i] & 0xFF);
    }
    return result;
}

From source file:Main.java

public static ByteBuffer makeByteBuffer(byte[] array) {
    final int SIZE = Byte.SIZE / 8;
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(array.length * SIZE);
    byteBuffer.order(ByteOrder.nativeOrder());
    byteBuffer.put(array);/*  ww w .  j  av  a  2 s  .  com*/
    byteBuffer.position(0);
    return byteBuffer;
}