Convert Byte Array To Integer In Java

Convert Byte Array To Integer In Java | We will discuss how to convert byte array to integer in Java programming language. Both byte and int are of primitive data type in Java but the difference is the size, and also here Integer belongs to the wrapper class.

The wrapper classes help us to use primitive data types as objects. Here we will be converting a byte array into an integer which is an array of bytes. In order words, a series of byte values will be converted into an integer. The array is a non-primitive data type that can have a continuous value of a similar data type. We can use two different ways to convert a byte array to an integer. The two different methods are as follows:-

  1. getInt()
  2. ByteBuffer.wrap()

Byte Array To Int Java

In the below program, we use getInt() method in order to convert the byte array to int, the getInt() method reads the next four bytes in the class as an integer.

import java.nio.ByteBuffer;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        byte[] bytearray = { 0x02, 0x06, 0x04, 0x09 };
        System.out.println("Byte array: " + Arrays.toString(bytearray));

        int number = ByteBuffer.wrap(bytearray).getInt();
        System.out.println("Number: " + number);
    }
}

Output:-

Byte array: [2, 6, 4, 9]
Number: 33948681

How does it work? The input byte array { 0x02, 0x06, 0x04, 0x09 } will yield the int value for 0x02060409.

Byte Array To Integer in Java

The getInt() method returns an int primitive value. But if we want an Integer value then we can assign the return value directly to an Integer type variable. Due to autoboxing int value is converted to an Integer value.

Integer number = ByteBuffer.wrap(bytearray).getInt();

For example, the input byte array {0x12, 0x13, 0x14, 0x15, 0x33} would yield the int value 0x12131415.

import java.nio.ByteBuffer;

public class Main {
    public static void main(String[] args) {
        byte[] bytearray = { 0x02, 0x06, 0x04, 0x09, 0x11 };
        Integer number = ByteBuffer.wrap(bytearray).getInt();
        System.out.println(number);
    }
}

Output:-

33948681

Byte Array To Integer Java

Here we are using ByteBuffer.wrap() method to convert the array to an integer in Java. This method wraps the array into the buffer and by default, it stores the value in the form of a big-endian “0x00” type. The wrap() method is in ByteBuffer class and hence we need to import the package “import java.nio.ByteBuffer”.

import java.nio.ByteBuffer;
import java.util.Arrays;

public class Main {
    static int[] byteArrayToInt(byte array[]) {
        ByteBuffer bytebuffer = ByteBuffer.wrap(array);
        short num = bytebuffer.getShort();

        ByteBuffer bytebuffer1 = ByteBuffer.allocate(2);
        bytebuffer1.putShort(num);

        byte[] byteArray = bytebuffer1.array();
        int[] intArray = new int[byteArray.length];
        for (int i = 0; i < intArray.length; i++) {
            intArray[i] = byteArray[i];
        }

        return intArray;
    }

    public static void main(String[] args) {
        byte[] bytesArray = { 0x02, 0x09 };
        int[] intArray = byteArrayToInt(bytesArray);
        System.out.println("Int array: " + Arrays.toString(intArray));
    }
}

Output:-

Int array: [2, 9]

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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