Converts a byte array to an integer. - Java File Path IO

Java examples for File Path IO:Byte Array

Description

Converts a byte array to an integer.

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(byteArrayToInteger(b));
    }/*from  ww  w . jav  a  2  s.c o  m*/

    /**
     *Converts a byte array to an integer.
     * @param b - The byte array. Must be a length of at least 4.
     * @return The integer.
     */
    public static int byteArrayToInteger(byte[] b) {
        return b[3] & 0xFF | (b[2] & 0xFF) << 8 | (b[1] & 0xFF) << 16
                | (b[0] & 0xFF) << 24;
    }
}

Related Tutorials