I need to translate a Java method, which uses the ByteBuffer object to C. How can I accurately replicate the code below using Java's ByteBuffer? And what data types should I use to embed floats in a byte array (in C)?
The code I spoke about:
public void example(float[] floatData) {
//Initialize byte array "byteData"
byte[] byteData = new byte[floatData.length * 4];
ByteBuffer byteDataBuffer = ByteBuffer.wrap(byteData);
byteDataBuffer.order(ByteOrder.nativeOrder());
//Fill byte array with data from floatData
for (int i = 0; i < floatData.length; i++)
byteDataBuffer.putFloat(floatData[i]);
//Concat length of array (as byte array) to "byteData"
byte[] vL = intToByteArray(floatData.length / 2);
byte[] v = concatArrays(vL, byteData);
//Fill the remaining array with empty bytes
if (v.length < 1024) {
int zeroPad = 1024 - v.length;
byte[] zeroArray = new byte[zeroPad];
v = concatArrays(v, zeroArray);
zeroArray = null;
}
//[Do something with v[] here...]
}
FloatData[] can look something like this: 1.00052387686001,-1.9974419759404,0.996936345285375
callocto allocate the space (1024). Set the length as the first sizeof(int) bytes, then usememcpyto copy the float array over to the rest of the allocated memory (sizeof(float)*length). I don't see ap[]anywhere?