Java Byte Array to Int bytesToInteger(byte[] b, int off)

Here you can find the source of bytesToInteger(byte[] b, int off)

Description

bytes To Integer

License

Open Source License

Declaration

public final static int bytesToInteger(byte[] b, int off) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004,2009 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://  w w w .j  a  v a  2  s  .co m
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * Assemble four bytes to an int value, make sure that the passed bytes
     * length is larger than 4.
     * 
     * @param bytes
     * @return int value of bytes
     */
    public final static int bytesToInteger(byte[] b) {
        assert b.length >= 4;
        return ((b[0] & 0xFF) << 24) + ((b[1] & 0xFF) << 16) + ((b[2] & 0xFF) << 8) + ((b[3] & 0xFF) << 0);
    }

    public final static int bytesToInteger(byte[] b, int off) {
        assert b.length - off >= 4;
        return ((b[off++] & 0xFF) << 24) + ((b[off++] & 0xFF) << 16) + ((b[off++] & 0xFF) << 8)
                + ((b[off] & 0xFF) << 0);
    }
}

Related

  1. bytesToInt(int off, byte... arr)
  2. bytesToInt16(byte highByte, byte lowByte)
  3. bytesToInt2(byte[] arr, int offset)
  4. bytesToIntArr(byte[] arr, int offset, int[] num, int soff, int size)
  5. bytesToInteger(byte[] b)
  6. bytesToInteger(byte[] bytes)
  7. bytesToInts(byte[] a, int ao, int[] b, int bo, int len)
  8. bytesToInts(byte[] bytes, int shift, int[] spec)
  9. bytesToInts(byte[] data)