Well this question may have been asked before but I could not find an exact match so I will give it a try.
I want to cast a float[] to a byte[] in way a single float would be cast (with loss of information etc), meaning I want to keep only the last 8bits of each float in the array. I found some code involving ByteBuffer, but this approach converted all 32bit of each float to 4 bytes (of 8 bits each) which is not the desired output (if someone is interested though, the link is http://www.javalobby.org/java/forums/t18962.html)
I did not find any code for this casting so I just used a classic for loop:
public static byte[] float2ByteArray(float floatArray[]) {
byte[] byteArray = new byte[floatArray.length];
for(int i = 0; i < floatArray.length; i++) {
byteArray[i] = (byte) floatArray[i];
}
return byteArray;
}
My question is if there is a more efficient way to achieve the same result that I am not aware of?