Java Byte Array to Int bytesToInt(final byte[] bytes)

Here you can find the source of bytesToInt(final byte[] bytes)

Description

bytes To Int

License

LGPL

Declaration

public static int bytesToInt(final byte[] bytes) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    public static int bytesToInt(final byte[] bytes) {
        assertNotNull("bytes", bytes);
        if (bytes.length != 4)
            throw new IllegalArgumentException("bytes.length != 4");

        return bytesToInt(bytes, 0);
    }//from w  w  w.  j av  a  2s  .  co m

    public static int bytesToInt(final byte[] bytes, final int index) {
        assertNotNull("bytes", bytes);
        if (bytes.length - index < 4)
            throw new IllegalArgumentException("bytes.length - index < 4");

        int value = 0;
        for (int i = 0; i < 4; ++i)
            value |= ((long) (bytes[index + i] & 0xff)) << (8 * (4 - 1 - i));

        return value;
    }

    public static final <T> T assertNotNull(final String name, final T object) {
        if (object == null)
            throw new IllegalArgumentException(String.format("%s == null", name));

        return object;
    }
}

Related

  1. bytesToInt(byte[] data)
  2. bytesToInt(byte[] value)
  3. bytesToInt(final byte[] arr)
  4. bytesToInt(final byte[] b)
  5. bytesToInt(final byte[] buf, final int offset)
  6. bytesToInt(final byte[] bytes)
  7. bytesToInt(final byte[] bytes, final int position, final int length, final int bitShiftStart, final int bitShitIncrement)
  8. bytesToInt(int off, byte... arr)
  9. bytesToInt16(byte highByte, byte lowByte)