I am looking for a reliable and elegant way to extract four bytes from a byte from an array in the form of a Float.
I can get UInt32 with bits through something like this:
let data: [Byte] = [0x00, 0x00, 0x00, 0x40, 0x86, 0x66, 0x66, 0x00] let dataPtr = UnsafePointer<Byte>(data) let byteOffset = 3 let bits = UnsafePointer<UInt32>(dataPtr + byteOffset)[0].bigEndian
But I cannot find a good way to convert this to Float in Swift.
For example, in Java:
float f = Float.intBitsToFloat(bits)
or in C:
float f = *(float *)&bits;
I tried pouring dataPtr into a UnsafePointer float, but then the content issue is the issue.
Any suggestions?
source share