I'm trying to convert a byte[] to a float[] by putting the byte[] in a ByteBuffer, converting this to a FloatBuffer (.asFloatBuffer
), and then converting this to an array.
private static float[] toFloatArray(byte[] bytes) {
ByteBuffer buffer = ByteBuffer.wrap(bytes);
return buffer.asFloatBuffer().array();
}
However running:
byte[] bytes = {14,32,26,21};
toFloatArray(bytes);
Gives me a java.lang.UnsupportedOperationException
at java.nio.FloatBuffer.array(Unknown Source)
.
I believe the documentation says that the error has something to do with the buffer not being backed by an array (???).
Anyone has an idea how to fix this, or how I SHOULD convert this array to floats?