Java Byte Array to Int bytesToInt(byte[] b)

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

Description

Converts a byte array into an integer.

License

Open Source License

Parameter

Parameter Description
b an array of bytes of length >= 4

Return

the corresponding integer

Declaration

public static int bytesToInt(byte[] b) 

Method Source Code

//package com.java2s;
/**//  w  ww  .  j a v  a2  s . c  o  m
 * OrbisGIS is a GIS application dedicated to scientific spatial simulation.
 * This cross-platform GIS is developed at French IRSTV institute and is able to
 * manipulate and create vector and raster spatial information.
 *
 * OrbisGIS is distributed under GPL 3 license. It is produced by the "Atelier SIG"
 * team of the IRSTV Institute <http://www.irstv.fr/> CNRS FR 2488.
 *
 * Copyright (C) 2007-2012 IRSTV (FR CNRS 2488)
 *
 * This file is part of OrbisGIS.
 *
 * OrbisGIS is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * OrbisGIS is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * OrbisGIS. If not, see <http://www.gnu.org/licenses/>.
 *
 * For more information, please consult: <http://www.orbisgis.org/>
 * or contact directly:
 * info_at_ orbisgis.org
 */

public class Main {
    private static final int BYTESIZE = 0xFF;

    /**
     * Converts a byte array into an integer.
     * 
     * Byte order: big endian
     *
     * Only the first four bytes of the argument are used.
     * @param b an array of bytes of length >= 4
     * @return the corresponding integer
     */
    public static int bytesToInt(byte[] b) {
        if (b.length < 4) {
            throw new IllegalArgumentException("The length of the byte array should be >= 4");
        }
        return ((b[3] & BYTESIZE)) + ((b[2] & BYTESIZE) << 8) + ((b[1] & BYTESIZE) << 16)
                + ((b[0] & BYTESIZE) << 24);
    }
}

Related

  1. bytesToInt(byte low, byte high)
  2. bytesToInt(byte[] array, int offset)
  3. bytesToInt(byte[] array, int offset)
  4. bytesToInt(byte[] b)
  5. bytesToInt(byte[] b)
  6. bytesToInt(byte[] b)
  7. bytesToInt(byte[] b)
  8. bytesToInt(byte[] b, int i)
  9. bytesToInt(byte[] b, int offset)