Convert double to byte array in Java
Description
The following code shows how to convert double to byte array.
Example
/*from w ww . j a v a 2s .c om*/
import java.nio.ByteBuffer;
public class Main {
public static byte[] convertToByteArray(double value) {
byte[] bytes = new byte[8];
ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
buffer.putDouble(value);
return buffer.array();
}
public static void main(String[] argv) {
System.out.println(convertToByteArray(21.3F));
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »