Android Byte to Int Convert getInt(byte b1, byte b2, byte b3, byte b4)

Here you can find the source of getInt(byte b1, byte b2, byte b3, byte b4)

Description

Gets an int from four bytes, doing all the necessary swapping

License

Open Source License

Parameter

Parameter Description
b1 a byte
b2 a byte
b3 a byte
b4 a byte

Return

the integer value represented by the four bytes

Declaration

public static int getInt(byte b1, byte b2, byte b3, byte b4) 

Method Source Code

//package com.java2s;
/*********************************************************************
 *
 *      Copyright (C) 2002 Andrew Khan// w  w  w.ja v  a  2  s.  co m
 *
 * This library 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 2.1 of the License, or (at your option) any later version.
 *
 * This library 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 this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 ***************************************************************************/

public class Main {
    /**
     * Gets an int from two bytes
     *
     * @param b2 the second byte
     * @param b1 the first byte
     * @return The integer value
     */
    public static int getInt(byte b1, byte b2) {
        int i1 = b1 & 0xff;
        int i2 = b2 & 0xff;
        int val = i2 << 8 | i1;
        return val;
    }

    /**
     * Gets an int from four bytes, doing all the necessary swapping
     *
     * @param b1 a byte
     * @param b2 a byte
     * @param b3 a byte
     * @param b4 a byte
     * @return the integer value represented by the four bytes
     */
    public static int getInt(byte b1, byte b2, byte b3, byte b4) {
        int i1 = getInt(b1, b2);
        int i2 = getInt(b3, b4);

        int val = i2 << 16 | i1;
        return val;
    }
}

Related

  1. getInt(byte b1, byte b2)
  2. makeInt(byte b3, byte b2, byte b1, byte b0)