Converting float array to hexadecimal string but only 1/4th of the data seems to exist when i load the data back

StackOverflow https://stackoverflow.com/questions/21394718

Question

I would love help figuring out why only a 4th of my data is being saved currently when I convert a float array to bytes , and that byte array then to a hexadecimal string .

I don't know if the issue is just my logic or even if its an issue with the data writing or retrieval so any and all help is appreciated

So I am retrieving an array of floats from my audio clip using the clip.GetData() function

I am then using the following function to convert those floats into bytes :

public byte[] ConvertFloatsToBytes(float[] audioData){ //** We create a new byte array to hold our clip data in bytes* byte[] bytes = new byte[audioData.Length * 4];

for ( int i = 0 ; i <audioData.Length ; i++){
                *//*** here we iterate though the existing data and place our bytes in a new array 
//***relative to the index of the audioData we are currently referencing
//*** A.K.A AudioData[0]'s data will exist in indexes 0,1,2, and 3 of the byte array*

   Buffer.BlockCopy(audioData,i,bytes,i*4,4);

 }

    return bytes;
}

Then I use the following function to convert those bytes into hexadecimal values :

public static string ByteArrayToString(byte[] ba) { string hex = BitConverter.ToString(ba);

    Debug.Log("ba.length = " + ba.Length.ToString() +"hex string = " + hex);
    return hex.Replace("-","");
}

when i get the data back I am using the following function to convert the data to bytes from our Hexadecimal values

public static byte[] StringToByteArray(String hex)
{
    int NumberChars = hex.Length;
    byte[] bytes = new byte[NumberChars / 2];
    for (int i = 0; i < NumberChars; i += 2)
        bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
    return bytes;
}

and lastly I use this function to convert my byte data back to float values

public float[] ConvertBytesToFloats(byte[] bytes){

        //*** Our float data should be a fourth of our byte arrays size , with 4 byte array elements holding the value of 1 float 

    `` //*** array element float[] floats = new float[bytes.Length/4];

    for(int i = 0 ; i < floats.Length ; i++){
    *//**** we  populate our float array by combining the bytes of our byte array*
       Buffer.BlockCopy(bytes, i*4, floats, i, 4);

            *//*** at this point we should have fully converted our float array to and from hex decimal*
    return floats;
}

I recently was given the save / load data management task by my team and considering we have very large arrays of audio data being generated not compressing these values is hammering me . I have found these functions and researched data management but i can't see where this issue is stemming from . if anybody with experience converting audio data to hex could help me i'd be very grateful ! As I said this code does work but only a fourth of our overall recording is actually saved and retrieved upon playing back the recording.

No correct solution

OTHER TIPS

I think the issue is that the second and third arguments are in bytes and should thus BOTH be 4*i, not i. See the msdn docs

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top