convert int from byte array - Android java.lang

Android examples for java.lang:array convert

Description

convert int from byte array

Demo Code


public class Main{

    /**//from ww w  .jav  a  2 s.  c om
     * convert int to byte array.
     * 
     * @param i
     * @return
     */
    public static byte[] int2ByteArray(int i) {
        byte[] result = new byte[4];
        // first 4 bit
        result[0] = (byte) ((i >> 24) & 0xff);
        // second 4 bit
        result[1] = (byte) ((i >> 16) & 0xff);
        // third 4 bit
        result[2] = (byte) ((i >> 8) & 0xff);
        // fourth 4 bit.
        result[3] = (byte) (i & 0xff);
        return result;
    }

}

Related Tutorials