Java Byte Array to Int bytesToInt(byte a)

Here you can find the source of bytesToInt(byte a)

Description

Convert a single byte to a 32-bit int, with sign extension.

License

Open Source License

Parameter

Parameter Description
a signed byte value

Return

32-bit integer

Declaration

public static int bytesToInt(byte a) 

Method Source Code

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

public class Main {
    /**//  w  w w  .j  a va2  s  .c o  m
     * Convert a single byte to a 32-bit int, with sign extension.
     * @param a signed byte value
     * @return 32-bit integer
     */
    public static int bytesToInt(byte a) {
        return (int) a; // whatever the high-order bit is set to is extended into integer 32-bit space
    }

    /**
     * Concatenate two bytes to a 32-bit int value.  <b>a</b> is the high order
     * byte in the resulting int representation, unless swapBytes is true, in
     * which <b>b</b> is the high order byte.
     * @param a high order byte
     * @param b low order byte
     * @param swapBytes byte order swap flag
     * @return 32-bit integer
     */
    public static int bytesToInt(byte a, byte b, boolean swapBytes) {
        // again, high order bit is expressed left into 32-bit form
        if (swapBytes) {
            return (a & 0xff) + ((int) b << 8);
        } else {
            return ((int) a << 8) + (b & 0xff);
        }
    }

    /**
     * Concatenate three bytes to a 32-bit int value.  Byte order is <b>a,b,c</b>
     * unless swapBytes is true, in which case the order is <b>c,b,a</b>.
     * @param a highest order byte
     * @param b second-highest order byte
     * @param c lowest order byte
     * @param swapBytes byte order swap flag
     * @return 32-bit integer
     */
    public static int bytesToInt(byte a, byte b, byte c, boolean swapBytes) {
        if (swapBytes) {
            return (a & 0xff) + ((b & 0xff) << 8) + ((int) c << 16);
        } else {
            return ((int) a << 16) + ((b & 0xff) << 8) + (c & 0xff);
        }
    }

    /**
     * Concatenate four bytes to a 32-bit int value.  Byte order is <b>a,b,c,d</b>
     * unless swapBytes is true, in which case the order is <b>d,c,b,a</b>.
     * <i>Note:</i> This method will accept unsigned and signed byte
     * representations, since high bit extension is not a concern here.
     * Java does not support unsigned integers, so the maximum value is not as
     * high as would be the case with an unsigned integer.  To hold an unsigned
     * 32-bit value, use uBytesToLong().
     * @param a highest order byte
     * @param b second-highest order byte
     * @param c second-lowest order byte
     * @param d lowest order byte
     * @param swapBytes byte order swap flag
     * @return 32-bit integer
     * see edu.iris.Fissures.seed.util.Utility#uBytesToLong(byte,byte,byte,byte,boolean)
     */
    public static int bytesToInt(byte a, byte b, byte c, byte d, boolean swapBytes) {
        if (swapBytes) {
            return ((a & 0xff)) + ((b & 0xff) << 8) + ((c & 0xff) << 16) + ((d & 0xff) << 24);
        } else {
            return ((a & 0xff) << 24) + ((b & 0xff) << 16) + ((c & 0xff) << 8) + ((d & 0xff));
        }
    }
}

Related

  1. bytes2IntArray(byte[] bytes)
  2. bytes2Integer(byte[] byteVal)
  3. bytes2LengthToIntLowOrder(byte[] intBytes)
  4. bytes2ToInt(byte[] inputValues)
  5. Bytes2Uint32(byte[] sour, int offset)
  6. bytesToInt(byte A, byte B, byte C, byte D)
  7. bytesToInt(byte abyte0[], int i, int j)
  8. bytesToInt(byte b1, byte b2, byte b3, byte b4)
  9. bytesToInt(byte bytes[], int start)