Java Integer Create toInteger(final byte[] data)

Here you can find the source of toInteger(final byte[] data)

Description

Converts the 4-byte array to a Big Endian int.

License

Open Source License

Parameter

Parameter Description
data 4-byte array to convert

Return

Big Endian int representation of the byte array

Declaration

public static int toInteger(final byte[] data) 

Method Source Code

//package com.java2s;
/*//  ww  w  .j ava  2s  . c o  m
 * File:                HashFunctionUtil.java
 * Authors:             Kevin R. Dixon
 * Company:             Sandia National Laboratories
 * Project:             Cognitive Foundry
 *
 * Copyright Jan 26, 2011, Sandia Corporation.
 * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
 * license for use of this work by or on behalf of the U.S. Government.
 * Export of this program may require a license from the United States
 * Government. See CopyrightHistory.txt for complete details.
 *
 */

public class Main {
    /**
     * Converts the 4-byte array to a Big Endian int.
     * @param data
     * 4-byte array to convert
     * @return
     * Big Endian int representation of the byte array
     */
    public static int toInteger(final byte[] data) {
        if (data.length != 4) {
            throw new IllegalArgumentException("data must be of length 4");
        }
        return toInteger(data, 0);
    }

    /**
     * Converts the 4 bytes in the given array starting at "offset"
     * to a Big Endian int.
     * @param data
     * Array to convert
     * @param offset
     * Offset into the array
     * @return
     * Big Endian int representation of the byte array at offset
     */
    public static int toInteger(final byte[] data, final int offset) {
        return ((data[offset] & 0xff) << 24) | ((data[offset + 1] & 0xff) << 16) | ((data[offset + 2] & 0xff) << 8)
                | ((data[offset + 3] & 0xff));
    }
}

Related

  1. toInteger(byte[] bytes)
  2. toInteger(byte[] bytes)
  3. toInteger(byte[] bytes, int index)
  4. toInteger(byte[] input)
  5. toInteger(E val)
  6. toInteger(final Double value)
  7. toInteger(final Object o)
  8. toInteger(final Object value)
  9. toInteger(final String bitString)