Thursday, 15 March 2012

How to convert 4 bytes to a Swift float? -



How to convert 4 bytes to a Swift float? -

i'm writing msgpack parser in swift way larn language. doesn't sense suited task i've been making progress. i've nail block can't seem convince convert 4 bytes float.

var bytes:array<uint8> = [0x9a, 0x99, 0x99, 0x41] //19.20000 var f:float = 0 memccpy(&f, &bytes, 4, 4) print(f)

in playground get:

fatal error: can't disclose optional.none playground execution failed: error: execution interrupted, reason: exc_bad_instruction (code=exc_i386_invop, subcode=0x0).

any ideas seek next?

drop & on "&bytes". bytes array.

var bytes:array<uint8> = [0x9a, 0x99, 0x99, 0x41] //19.20000 var f:float = 0.0 memccpy(&f, bytes, 4, 4) // per op. memcpy(&f, bytes, 4) preferred println ("f=\(f)")// f=19.2000007629395

swift swift-playground

No comments:

Post a Comment