Java Byte Array to Int bytesToInt(byte[] buf, int offset)

Here you can find the source of bytesToInt(byte[] buf, int offset)

Description

Converts a 4 byte array of unsigned bytes to a singed long.

License

Open Source License

Parameter

Parameter Description
b an array of 4 unsigned bytes

Return

an int representing the 4 bytes

Declaration

public static final int bytesToInt(byte[] buf, int offset) 

Method Source Code

//package com.java2s;
/* This file is part of VoltDB.
 * Copyright (C) 2008-2013 VoltDB Inc.//w w  w.  j  ava 2s . co  m
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with VoltDB.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Converts a 4 byte array of unsigned bytes to a singed long.
     * Note that this assumes a positive result.
     * @param b an array of 4 unsigned bytes
     * @return an int representing the 4 bytes
     */
    public static final int bytesToInt(byte[] buf, int offset) {
        int retval = 0;
        int i = 0;
        for (; i < 3; ++i) {
            retval |= buf[i + offset] & 0xFF;
            retval <<= 8;
        }
        retval |= buf[i + offset] & 0xFF;
        return retval;
    }
}

Related

  1. bytesToInt(byte[] b)
  2. bytesToInt(byte[] b)
  3. bytesToInt(byte[] b, int i)
  4. bytesToInt(byte[] b, int offset)
  5. bytesToInt(byte[] b, int offset)
  6. bytesToInt(byte[] buf, int startIdx)
  7. bytesToInt(byte[] buff, int off, int len)
  8. bytesToInt(byte[] bytes)
  9. bytesToInt(byte[] bytes)