Convert long value to byte array in Java
Description
The following code shows how to convert long value to byte array.
Example
/*from ww w .ja v a 2s . co m*/
import java.nio.ByteBuffer;
public class Main {
public static byte[] convertToByteArray(long value) {
byte[] bytes = new byte[8];
ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
buffer.putLong(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 »