Convert a byte array to an integer in Java
This post will discuss how to convert a byte array to an integer in Java.
If you are working with binary data in Java, you may need to convert a byte array to an integer value, or vice versa. For example, you may want to store, transmit, or manipulate encryption keys, hashes, or bitmaps. There are several ways to peform this conversion in Java:
1. Using ByteBuffer class
You can use the ByteBuffer class from the java.nio package, which provides methods for reading and writing primitive values from and to byte arrays. You can create a ByteBuffer object from a byte array using the wrap() method, and then use the getInt() method to get an int value from the buffer. You can also specify the endianness of the buffer using the order() method. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.nio.ByteBuffer; import java.nio.ByteOrder; class Main { public static void main(String[] args) { byte[] bytes = { (byte) 0x00, (byte) 0xEF, (byte) 0xCA, (byte) 0xCE }; // create a buffer from the byte array ByteBuffer buffer = ByteBuffer.wrap(bytes); // set the endianness of the buffer buffer.order(ByteOrder.BIG_ENDIAN); // get an int value from the buffer int intValue = buffer.getInt(); System.out.println(intValue); // prints 15715022 } } |
The disadvantage of using this approach is requires us to allocate a new ByteBuffer instance for each conversion, which can be inefficient and wasteful.
2. Using BigInteger class
You can also use the BigInteger class from the java.math package, which provides methods for converting arbitrary-length byte arrays to and from integer values. You can create a BigInteger object from a byte array using the constructor that takes a byte array and a signum as arguments. The signum indicates whether the value is positive or negative. You can then use the intValue() method to get an int value from the BigInteger object. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.math.BigInteger; class Main { public static void main(String[] args) { byte[] bytes = { (byte) 0x00, (byte) 0xEF, (byte) 0xCA, (byte) 0xCE }; // create a BigInteger object from the byte array and signum BigInteger bigInt = new BigInteger(1, bytes); // get an int value from the BigInteger object int intValue = bigInt.intValue(); System.out.println(intValue); // prints 15715022 } } |
3. Using custom method
Finally, you can write your own custom method that uses bitwise operators and loops to extract each byte from the byte array and combine them into an int value. You need to be careful about handling negative values and different endianness. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class Main { public static int fromByteArray(byte[] bytes) { if (bytes == null || bytes.length != 4) { throw new IllegalArgumentException("Invalid byte array"); } int value = 0; for (int i = 0; i < 4; i++) { // shift left by 8 bits and add the next byte value = (value << 8) | (bytes[i] & 0xFF); } return value; } public static void main(String[] args) { byte[] bytes = { (byte) 0x00, (byte) 0xEF, (byte) 0xCA, (byte) 0xCE }; int intValue = fromByteArray(bytes); System.out.println(intValue); // prints 15715022 } } |
The disadvantage of using this approach is that it requires more code and less readability. It also requires us to understand and remember the bit manipulation operations and their precedence rules, which can be complex and confusing.
You can also use third party libraries such as Guava to convert a byte array to an integer in Java. You can use the Ints.fromByteArray() utility method that converts a byte array into an int value using big-endian order, which is discussed in this post.
That’s all about converting a byte array to an integer in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)