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

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

Description

Unmarshal a byte array to an integer.

License

Open Source License

Parameter

Parameter Description
array The array of bytes.
offset The offset from which to start unmarshalling.

Declaration

public static int bytesToInt(byte[] array, int offset) 

Method Source Code

//package com.java2s;
/*/*w  w  w  .ja  va  2 s .  c  o  m*/
 * %W% %E%
 *
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

public class Main {
    /** Unmarshal a byte array to an integer.
        Assume the bytes are in BIGENDIAN order.
        i.e. array[offset] is the most-significant-byte
        and  array[offset+3] is the least-significant-byte.
        @param array The array of bytes.
        @param offset The offset from which to start unmarshalling.
     */
    public static int bytesToInt(byte[] array, int offset) {
        int b1, b2, b3, b4;

        b1 = (array[offset++] << 24) & 0xFF000000;
        b2 = (array[offset++] << 16) & 0x00FF0000;
        b3 = (array[offset++] << 8) & 0x0000FF00;
        b4 = (array[offset++] << 0) & 0x000000FF;

        return (b1 | b2 | b3 | b4);
    }
}

Related

  1. bytesToInt(byte abyte0[], int i, int j)
  2. bytesToInt(byte b1, byte b2, byte b3, byte b4)
  3. bytesToInt(byte bytes[], int start)
  4. bytesToInt(byte low, byte high)
  5. bytesToInt(byte[] array, int offset)
  6. bytesToInt(byte[] b)
  7. bytesToInt(byte[] b)
  8. bytesToInt(byte[] b)
  9. bytesToInt(byte[] b)