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

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

Description

convert given byte array to integer

License

Open Source License

Parameter

Parameter Description
bytes a parameter

Return

integer represented by the given byte array

Declaration

public static int bytesToInt(final byte[] bytes) 

Method Source Code

//package com.java2s;
/**/*from w  w w .  j  a v  a2s.c  o m*/
 * Created on Oct 21, 2010 This file is part of JObexFTP 2.0, and it contains
 * parts of OBEX4J.
 *
 * JObexFTP 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.
 *
 * JObexFTP 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 JObexFTP. If not, see <http://www.gnu.org/licenses/>.
 *
 */

public class Main {
    /**
     * convert given byte array to integer
     *
     * @param bytes
     * @return integer represented by the given byte array
     */
    public static int bytesToInt(final byte[] bytes) {
        int result = 0;
        if (bytes == null) {
            return 0;
        }
        for (int i = 0; i < bytes.length; i++) {
            int temp = (int) bytes[i];
            if (temp < 0) {
                temp = 0x100 + temp;
            }
            result += temp << (8 * (bytes.length - 1 - i));
        }

        return result;
    }
}

Related

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