Unable to read sphericalHarmonicsCoefficients

Hi All,


I am trying to read the "sphericalHarmonicsCoefficients: Data" object. I can print and see that the number of bytes is 108 in the retrieved object.

I use the below code to read the "Data" object into a "Float" array.


private func procSph(sphHar: Data) -> Array<Float32>
    {
        var sphCoeff = [Float]()
        var bytes = [UInt8]()
        var tmpByte: UInt8
        var tmpSph: Data
        var tmpFloat:Float = 0.0
        var cnt:Int = 0
        
        tmpSph = sphHar
        
        for i in 0...107
        {
            cnt = cnt + 1
            tmpByte = tmpSph.remove(at: i)
            bytes.append(tmpByte)
            if(cnt == 4)
            {
                memcpy(&tmpFloat, bytes, 4)
                sphCoeff.append(tmpFloat)
                cnt = 0
                bytes.removeAll()
            }
        }
        return sphCoeff
    }


So basically I am extracting 4 bytes, converting them to "Float" storing them into array. The above code works fine till the 54th byte, and then it gives an exception in line no. 16 as I try to read the 55th byte.


Thanks for your help in advance _/\_


Best Regards,

Sumit

Accepted Reply

The below code worked,


return sphHar.withUnsafeBytes { rawBytes -> [Float32] in
  return [Float32](rawBytes.bindMemory(to: Float32.self))
}

Replies

I am not sure if it is a byte concatenating issue since I am able to generate and store 13 Float elements (52 bytes) in the array. It is only when I am trying to generate the next array element that I get this issue.
Please let me know if this is the right way to read the elements of the "Data" object or I am doing something fundamentally wrong?

The below code worked,


return sphHar.withUnsafeBytes { rawBytes -> [Float32] in
  return [Float32](rawBytes.bindMemory(to: Float32.self))
}