1

Pardon if I'm overlooking a quick fix here, but I've read and worked through a lot of other answers dealing with converting float[] to byte[] and back to float[], but still can't figure out why I'm getting unexpected results from the code below. I'm converting a float[] to byte[] for storage as a blob in a SQLite database. However, before even loading my byte[] to the database I checked to make sure I could accurately convert it back to a float[], however the results I get don't match the float[] I converted. I did look into the endianness issue and made a catch for that (it's commented out below), but the results while different were still unexpected. Any nudge in the right direction would be appreciated - this is new territory for me. Thanks!

The test array of 5 vector3's should each read: (1,2,3) instead I get: (0,1,2), (0,0,0), (0,0,0), (0,0,2), (1,2,3)

    //////////
    // input
    var numPositions = 5;
    var particlePositionsArrayForEachSegment = new Vector3[numPositions];
    
    for (int i = 0; i < particlePositionsArrayForEachSegment.Length; i++)
    {
        Vector3 vector = new Vector3(1f, 2f, 3f);
        particlePositionsArrayForEachSegment[i] = vector;
    }

    byte[] buff = new byte[particlePositionsArrayForEachSegment.Length * (sizeof(float) * 3)];
    for (int j = 0; j < particlePositionsArrayForEachSegment.Length; j++)
    {
        /*if (BitConverter.IsLittleEndian)
        {
            // x
            byte[] xBytes = BitConverter.GetBytes(particlePositionsArrayForEachSegment[j].x);
            Array.Reverse(xBytes);
            Buffer.BlockCopy(xBytes, 0, buff, j + 0 * sizeof(float), sizeof(float));
            // y
            byte[] yBytes = BitConverter.GetBytes(particlePositionsArrayForEachSegment[j].y);
            Array.Reverse(yBytes);
            Buffer.BlockCopy(yBytes, 0, buff, j + 1 * sizeof(float), sizeof(float));
            // z
            byte[] zBytes = BitConverter.GetBytes(particlePositionsArrayForEachSegment[j].z);
            Array.Reverse(zBytes);
            Buffer.BlockCopy(zBytes, 0, buff, j + 2 * sizeof(float), sizeof(float));
        }
        else
        {*/
            Buffer.BlockCopy(BitConverter.GetBytes(particlePositionsArrayForEachSegment[j].x), 
                0, buff, j + 0 * sizeof(float), sizeof(float));
            Buffer.BlockCopy(BitConverter.GetBytes(particlePositionsArrayForEachSegment[j].y), 
                0, buff, j + 1 * sizeof(float), sizeof(float));
            Buffer.BlockCopy(BitConverter.GetBytes(particlePositionsArrayForEachSegment[j].z),
                0, buff, j + 2 * sizeof(float), sizeof(float));
        //}
        
    }
    
    //////////
    // output
    var outputParticlePositionsArrayForEachSegment = new Vector3[numPositions];
    byte[] buff1 = buff;
  
    for (int k = 0; k < outputParticlePositionsArrayForEachSegment.Length; k++)
    {
        Vector3 vect = Vector3.zero;
        vect.x = BitConverter.ToSingle(buff1, k + 0 * sizeof(float));
        vect.y = BitConverter.ToSingle(buff1, k + 1 * sizeof(float));
        vect.z = BitConverter.ToSingle(buff1, k + 2 * sizeof(float));
        outputParticlePositionsArrayForEachSegment[k] = vect;
    }
    
    var alleleArray = outputParticlePositionsArrayForEachSegment;
    if (alleleArray != null)
        for (var l = 0; l < alleleArray.Length; l++)
        {
            Debug.Log(alleleArray[l]);
        }
}
gromiczek
  • 2,970
  • 5
  • 28
  • 49

1 Answers1

0

Your problem is with the indices used in copying and converting. It should be (3 * j + 0) instead of j + 0 for both copying and converting.

Sohaib Jundi
  • 1,576
  • 2
  • 7
  • 15