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