Java Integer Create toInt(byte[] bytes, int offset)

Here you can find the source of toInt(byte[] bytes, int offset)

Description

Convert a byte array to an int value

License

Open Source License

Parameter

Parameter Description
bytes a parameter
offset a parameter

Return

the int value corresponding to the 4 first bytes of the byte array

Declaration

public static int toInt(byte[] bytes, int offset) 

Method Source Code

//package com.java2s;
/*//w  ww .  j a  va2 s  . c o m
 * This file is part of org.kalmeo.util.
 * 
 * org.kalmeo.util 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.
 * 
 * org.kalmeo.util 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 org.kalmeo.util.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * Creation date : 17 janv. 08
 * Copyright (c) Kalmeo 2007-2008. All rights reserved.
 * http://www.kalmeo.org
 */

public class Main {
    /**
     * Convert a byte array to an int value
     * 
     * @param bytes
     * @param offset
     * @return the int value corresponding to the 4 first bytes of the byte
     *         array
     */
    public static int toInt(byte[] bytes, int offset) {
        int value = 0;
        if (bytes != null && bytes.length >= 4) {
            for (int i = 0; i < 4; ++i) {
                value += (0x000000FF & bytes[offset + i]) << ((3 - i) * 8);
            }
        }
        return value;
    }
}

Related

  1. toInt(byte[] bytes)
  2. toInt(byte[] bytes)
  3. toInt(byte[] bytes)
  4. toInt(byte[] bytes, boolean isBigEndian)
  5. toInt(byte[] bytes, int index)
  6. toInt(byte[] bytes, int offset)
  7. toInt(byte[] bytes, int offset)
  8. toInt(byte[] bytes, int offset)
  9. toInt(byte[] data)