float to byte[]
Reverse a byte[] in Java:
public static byte[] reverse(byte[] arr)
{
byte[] buf = new byte[arr.length];
int len = arr.length;
for (int i = 0; i < len; i++) {
buf[i] = arr[len - 1 - i];
}
return buf;
}Java float -> byte[] :
public static byte[] floatToByteArray(float value)
{
int intBits = Float.floatToIntBits(value);
return new byte[] {
(byte) (intBits >> 24),
(byte) (intBits >> 16),
(byte) (intBits >> 8),
(byte) (intBits)
};
}A more elegant way one-liner to do float -> byte[] :
ByteBuffer.allocate(4).putFloat(value).array();Java byte[] -> float
byte b[] = new byte[4];
ByteBuffer buffer = ByteBuffer.wrap(b);
float fval = buffer.getFloat();Last updated
Was this helpful?