Java Byte Array to Int bytesToInt(byte[] array, int offset)

Here you can find the source of bytesToInt(byte[] array, int offset)

Description

Extracts the big-endian integer starting at offset from array.

License

Open Source License

Parameter

Parameter Description
array The array to extract data from.
offset The offset in the array of the most significant byte.

Return

The integer extracted from the array.

Declaration

public static int bytesToInt(byte[] array, int offset) 

Method Source Code

//package com.java2s;
/*//from w  w w . j  a v a 2  s .  c om
 * Copyright 2013-2014 Colby Skeggs
 *
 * This file is part of the CCRE, the Common Chicken Runtime Engine.
 *
 * The CCRE is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * The CCRE 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 Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with the CCRE.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Extracts the big-endian integer starting at offset from array. This is
     * equivalent to:
     * <code>((array[offset] &amp; 0xff) &lt;&lt; 24) | ((array[offset+1] &amp; 0xff) &lt;&lt; 16) | ((array[offset+2] &amp; 0xff) &lt;&lt; 8) | (array[offset+3] &amp; 0xff)</code>
     *
     * @param array The array
     *
     *
     *
     *
     * to extract data from.
     * @param offset The offset in the array of the most significant byte.
     * @return The integer extracted from the array.
     */
    public static int bytesToInt(byte[] array, int offset) {
        int highWord = ((array[offset] & 0xff) << 24) | ((array[offset + 1] & 0xff) << 16);
        int lowWord = ((array[offset + 2] & 0xff) << 8) | (array[offset + 3] & 0xff);
        return highWord | lowWord;
    }
}

Related

  1. bytesToInt(byte A, byte B, byte C, byte D)
  2. bytesToInt(byte abyte0[], int i, int j)
  3. bytesToInt(byte b1, byte b2, byte b3, byte b4)
  4. bytesToInt(byte bytes[], int start)
  5. bytesToInt(byte low, byte high)
  6. bytesToInt(byte[] array, int offset)
  7. bytesToInt(byte[] b)
  8. bytesToInt(byte[] b)
  9. bytesToInt(byte[] b)