1. Overview

In this article we will discuss various techniques of converting int to byte array and vice versa,int array to byte array and so on.In java int data type take 4 bytes and it’s range is -2,147,483,648 to 2,147,483, 647.

2. int to byte array

Here we have discussed four different ways to convert int to byte array in java as below:

2.1 BigInteger

private byte[] bigIntToByteArray( final int i ) {
    BigInteger bigInt = BigInteger.valueOf(i);      
    return bigInt.toByteArray();
}

2.2 DataOutputStream

private byte[] intToByteArray ( final int i ) throws IOException {      
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(bos);
    dos.writeInt(i);
    dos.flush();
    return bos.toByteArray();
}

2.3 ByteBuffer

private byte[] intToBytes( final int i ) {
    ByteBuffer bb = ByteBuffer.allocate(4); 
    bb.putInt(i); 
    return bb.array();
}

2.4 Shift operations

private static byte[] intToBytes(final int a) {
    return new byte[] {
        (byte)((data >> 24) & 0xff),
        (byte)((data >> 16) & 0xff),
        (byte)((data >> 8) & 0xff),
        (byte)((data >> 0) & 0xff),
    };
}

3. byte array to int

We can convert any byte array to int using below two techniques.

3.1 ByteBuffer

Java provide ByteBuffer class to do the same.to convert any byte array, first we need to wrap up using ByteBuffer’s static method wrap and then by calling getInt() method we can get integer value of that byte array.

private int convertByteArrayToInt(byte[] intBytes){
    ByteBuffer byteBuffer = ByteBuffer.wrap(intBytes);
    return byteBuffer.getInt();
}

3.2 Shift operations

Second way of convery byte array to int is by using shift operator and & operator.

private int convertByteArrayToInt(byte[] data) {
    if (data == null || data.length != 4) return 0x0;
    // ----------
    return (int)( // NOTE: type cast not necessary for int
            (0xff & data[0]) << 24  |
            (0xff & data[1]) << 16  |
            (0xff & data[2]) << 8   |
            (0xff & data[3]) << 0
            );
}

4. int array to byte array

private byte[] convertIntArrayToByteArray(int[] data) {
        if (data == null) return null;
        // ----------
        byte[] byts = new byte[data.length * 4];
        for (int i = 0; i < data.length; i++)
            System.arraycopy(convertIntToByteArray(data[i]), 0, byts, i * 4, 4);
        return byts;
    }

5. byte array to int array

public int[] convertByteArrayToIntArray(byte[] data) {
        if (data == null || data.length % 4 != 0) return null;
        // ----------
        int[] ints = new int[data.length / 4];
        for (int i = 0; i < ints.length; i++)
            ints[i] = ( convertByteArrayToInt(new byte[] {
                    data[(i*4)],
                    data[(i*4)+1],
                    data[(i*4)+2],
                    data[(i*4)+3],
            } ));
        return ints;
    }

5. Conclusion

In this article we have discussed various techniques of converting byte array to int, int to byte array, int array to byte array and byte array to int array using ByteBuffer,DataInputStream,DataOutputStream and some custom logic.

Refer ByteBuffer , DataInputStream for more details.

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

Your email address will not be published. Required fields are marked *