I'm trying to reconstruct a float from a binary stream in Swift according to this answer. As far as I can tell, the bytes are correct, but the resulting float is not.
func didReceive(data: NSData!) {
var x:Float = 0
var bytes:[UInt8] = [UInt8](count: 4, repeatedValue: 0)
data.getBytes(&bytes, range: NSMakeRange(0, 4))
memcpy(&x, bytes, 4)
NSLog("x:%f bytes:[%d, %d, %d, %d]", x, bytes[0], bytes[1], bytes[2], bytes[3]);
}
This function prints out the following:
x:0.000000 bytes:[25, 0, 0, 0]
When I inspect x
in the debugger, it reports the value as:
0.0000000000000000000000000000000000000000000350324616
However, the bytes [25,0,0,0]
should be hex 0x19000000
, which I think should translate to about 6.617
.
Where's my mistake here. Is a Swift float not a IEEE754 32 bit float?