Get a int from 4 bytes of the given array at offset 0. - Java java.lang

Java examples for java.lang:byte Array to int

Description

Get a int from 4 bytes of the given array at offset 0.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] b = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(bytes2int(b));
    }/*from  w w  w. j a va 2  s  .co m*/

    /**
     * Get a int from 4 bytes of the given array at offset 0.
     * 
     * @param b byte array
     * @return a short
     */
    public static final int bytes2int(byte[] b) {
        return bytes2int(b, 0);
    }

    /**
     * Get a int from 4 bytes of the given array at specific offset.
     * 
     * @param b byte array
     * @param off offset of the byte array
     * @return a short
     */
    public static final int bytes2int(byte[] b, int off) {
        return b[off] << 24 | (b[off + 1] & 0xff) << 16
                | (b[off + 2] & 0xff) << 8 | (b[off + 3] & 0xff);
    }
}

Related Tutorials