In this post, I am going to explain multiple approaches how to convert an integer to the byte array. Be cautious about the byte orders while converting the integer to byte array.
There are two standard byte array order formats.
a. BIG_ENDIAN: In this order, the bytes of a multibyte value are ordered from most significant to least significant.
b. LITTLE_ENDIAN: In this order, the bytes of a multibyte value are ordered from least significant to most significant.
Approach 1: Using custom business logic.
public static byte[] toByteArray(int i) {
byte[] result = new byte[4];
result[0] = (byte) (i >> 24);
result[1] = (byte) (i >> 16);
result[2] = (byte) (i >> 8);
result[3] = (byte) (i);
return result;
}
IntToByteArray.java
package com.sample.app.numbers;
public class IntToByteArray {
public static byte[] toByteArray(int i) {
byte[] result = new byte[4];
result[0] = (byte) (i >> 24);
result[1] = (byte) (i >> 16);
result[2] = (byte) (i >> 8);
result[3] = (byte) (i);
return result;
}
public static void printByteArray(byte[] byteArr) {
for (byte b : byteArr) {
System.out.print(b + "\t");
}
}
public static void main(String args[]) {
byte[] byteArr = toByteArray(12345678);
printByteArray(byteArr);
}
}
Output
0 -68 97 78
Approach 2: Using ByteBuffer.
public static byte[] toByteArray(int i) {
ByteBuffer b = ByteBuffer.allocate(4);
b.order(ByteOrder.BIG_ENDIAN);
b.putInt(i);
return b.array();
}
IntToByteArray1.java
package com.sample.app.numbers;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class IntToByteArray1 {
public static byte[] toByteArray(int i) {
ByteBuffer b = ByteBuffer.allocate(4);
b.order(ByteOrder.BIG_ENDIAN);
b.putInt(i);
return b.array();
}
public static void printByteArray(byte[] byteArr) {
for (byte b : byteArr) {
System.out.print(b + "\t");
}
}
public static void main(String args[]) {
byte[] byteArr = toByteArray(12345678);
printByteArray(byteArr);
}
}
Output
0 -68 97 78
One advantage of using ByteBuffer is, it gives flexibility to choose the byte order(BIG_ENDIAN, LITTLE_ENDIAN).
Approach 3: Using BigInteger#toByteArray method
BigInteger#toByteArray method return byte array containing the two's-complement representation of this BigInteger. The byte array will be in big-endian byte-order.
IntToByteArray2.java
package com.sample.app.numbers;
import java.math.BigInteger;
public class IntToByteArray2 {
public static byte[] toByteArray(int i) {
BigInteger bigInt = BigInteger.valueOf(i);
return bigInt.toByteArray();
}
public static void printByteArray(byte[] byteArr) {
for (byte b : byteArr) {
System.out.print(b + "\t");
}
}
public static void main(String args[]) {
byte[] byteArr = toByteArray(12345678);
printByteArray(byteArr);
}
}
Output
0 -68 97 78
Approach 4: Using DataOutputStream.
public static byte[] toByteArray(int i) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
dataOutputStream.writeInt(i);
dataOutputStream.flush();
return byteArrayOutputStream.toByteArray();
}
IntToByteArray3.java
package com.sample.app.numbers;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class IntToByteArray3 {
public static byte[] toByteArray(int i) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
dataOutputStream.writeInt(i);
dataOutputStream.flush();
return byteArrayOutputStream.toByteArray();
}
public static void printByteArray(byte[] byteArr) {
for (byte b : byteArr) {
System.out.print(b + "\t");
}
}
public static void main(String args[]) throws IOException {
byte[] byteArr = toByteArray(12345678);
printByteArray(byteArr);
}
}
Output
0 -68 97 78
No comments:
Post a Comment