Java Byte Array to Int bytes2int(byte[] bytes, int offset, boolean bigEndian)

Here you can find the source of bytes2int(byte[] bytes, int offset, boolean bigEndian)

Description

bytesint

License

Apache License

Declaration

public static int bytes2int(byte[] bytes, int offset, boolean bigEndian) 

Method Source Code

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

public class Main {
    public static int bytes2int(byte[] bytes, boolean bigEndian) {
        return bytes2int(bytes, 0, bigEndian);
    }// w w w.  j  a v  a 2 s . co  m

    public static int bytes2int(byte[] bytes, int offset, boolean bigEndian) {
        int val = 0;

        if (bigEndian) {
            val += (bytes[offset + 0] & 0xff) << 24;
            val += (bytes[offset + 1] & 0xff) << 16;
            val += (bytes[offset + 2] & 0xff) << 8;
            val += (bytes[offset + 3] & 0xff);
        } else {
            val += (bytes[offset + 3] & 0xff) << 24;
            val += (bytes[offset + 2] & 0xff) << 16;
            val += (bytes[offset + 1] & 0xff) << 8;
            val += (bytes[offset + 0] & 0xff);
        }

        return val;
    }
}

Related

  1. bytes2int(byte[] bytes)
  2. bytes2int(byte[] bytes)
  3. bytes2int(byte[] bytes, int begin)
  4. bytes2Int(byte[] bytes, int defaultValue)
  5. bytes2Int(byte[] bytes, int offset)
  6. bytes2int(byte[] in, int index1, int index2, int index3)
  7. bytes2int(byte[] src)
  8. bytes2Int(byte[] src, int start)
  9. Bytes2Int16(byte[] sour, int offset)