Convert int value to byte array in Java
Description
The following code shows how to convert int value to byte array.
Example
/*from w ww . ja va 2 s . c o m*/
import java.nio.ByteBuffer;
public class Main {
public static byte[] convertToByteArray(int value) {
byte[] bytes = new byte[4];
ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
buffer.putInt(value);
return buffer.array();
}
public static void main(String[] argv) {
System.out.println(convertToByteArray(10000));
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »