Swift: extract float from byte data

后端 未结 3 1361
醉话见心
醉话见心 2021-01-01 03:11

I\'m looking for a robust and elegant way to extract four big-endian bytes from an array as a Float.

I can get a UInt32 with the bits via something like this:

<
3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-01 04:06

    The equivalent Swift code is

    let flt = unsafeBitCast(bits, Float.self)
    

    which gives 4.2 with your data.

提交回复
热议问题