-3

I want to convert a byte array (byte[]) to float array (float[]) in Java.

I read some examples but when I convert back again, to compare that the result is same that original signal, it is not the same.

So how can I perform this process?

EDIT:

I do this:

ByteArrayInputStream bas = new ByteArrayInputStream(data);
        DataInputStream ds = new DataInputStream(bas);
        float[] data_proc = new float[numbytes / 4];

        for (int i = 0; i < data_proc.length; i++) {
            try {
                data_proc[i] = ds.readFloat();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }



        ByteArrayOutputStream bas2 = new ByteArrayOutputStream();
        DataOutputStream ds2 = new DataOutputStream(bas2);
        for(int i=0;i<data_proc.length;i++){

            try {
                ds2.writeFloat(data_proc[i]);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
            byte telo[] = bas2.toByteArray();

But when i compare two .pcm files not are equals.

user3243651
  • 241
  • 2
  • 4
  • 14

1 Answers1

0

It can be easily done by making use of the ByteBuffer class, it has putFloat method which you need to use to get your job done.

Example:

public static byte [] long2ByteArray (long value)
{
    return ByteBuffer.allocate(8).putLong(value).array();
}

public static byte [] float2ByteArray (float value)
{  
    return ByteBuffer.allocate(4).putFloat(value).array();
}
Badrul
  • 1,582
  • 4
  • 16
  • 27