Java Byte Array to Int byteToInt(byte[] bank)

Here you can find the source of byteToInt(byte[] bank)

Description

Convert byte[] bank of length 4 or less to integer.

License

Open Source License

Parameter

Parameter Description
bank byte[] bank

Return

integer from bytes

Declaration

public static int byteToInt(byte[] bank) 

Method Source Code

//package com.java2s;
/*//from   w  ww .  ja  v a  2 s.c o m
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Convert byte[] bank of length 4 or less to integer.
     * @param bank byte[] bank
     * @return integer from bytes
     */
    public static int byteToInt(byte[] bank) {
        if (bank.length > 4)
            return -1;
        int retval = 0;
        int up = 0;
        for (int i = bank.length - 1; i >= 0; i--) {
            retval += ((int) (bank[up] & 0xff) << (i * 8));
            up++;
        }

        return retval;
    }
}

Related

  1. bytesToInts(byte[] data)
  2. bytesToInts(byte[][] bytes)
  3. byteToInt(byte b[])
  4. byteToInt(byte[] b)
  5. byteToInt(byte[] b)
  6. byteToInt(byte[] buf, int off)
  7. byteToInt(byte[] byteArray)
  8. byteToInt(byte[] bytes)
  9. byteToInt(byte[] i_Value)