如何将4字节转换为Swift浮点数?

时间:2023-01-23 22:19:48

I'm writing a MsgPack parser in Swift as a way to learn the language. It doesn't feel very well suited to the task but I've been making progress. Now I've hit a block where I can't seem to convince it to convert 4 bytes into a float.

我正在用Swift编写一个MsgPack解析器,作为学习语言的一种方式。我觉得不太适合这项任务,但我已经取得了进展。现在我碰到了一个块,我似乎无法说服它将4个字节转换成浮点数。

var bytes:Array<UInt8> = [0x9A, 0x99, 0x99, 0x41] //19.20000

var f:Float = 0

memccpy(&f, &bytes, 4, 4)

print(f)

In the playground I get:

在操场上,我得到:

fatal error: Can't unwrap Optional.None Playground execution failed: error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0).

致命错误:不能打开可选。错误:执行被中断,原因:exc_bad_instructions(代码=EXC_I386_INVOP,子代码=0x0)。

Any ideas what to try next?

你知道下一步该怎么做吗?

2 个解决方案

#1


13  

Drop the & on &bytes. bytes is an array.

删除& on和bytes。字节是一个数组。

    var bytes:Array<UInt8> = [0x9A, 0x99, 0x99, 0x41] //19.20000

    var f:Float = 0.0

    memccpy(&f, bytes, 4, 4) // as per OP. memcpy(&f, bytes, 4) preferred

    println ("f=\(f)")// f=19.2000007629395

Update Swift 3

更新快3

memccpy does not seem to work in Swift 3. As commentators have said, use memcpy :

memccpy似乎在Swift 3中不起作用。正如评论人士所说,使用memcpy:

import Foundation
var bytes:Array<UInt8> = [0x9A, 0x99, 0x99, 0x41] //19.20000

var f:Float = 0.0

/* Not in Swift 3
 memccpy(&f, bytes, 4, 4) // as per OP.

 print("f=\(f)")// f=19.2
 */

memcpy(&f, bytes, 4) /

print("f=\(f)")// f=19.2

#2


2  

public func parseInt32(bytes:[UInt8], offset:Int)->Int32{

    var pointer = UnsafePointer<UInt8>(bytes)
    pointer = pointer.advancedBy(offset)

    let iPointer =  UnsafePointer<Int32>(pointer)
    return iPointer.memory

}

public func parseFloat32(bytes:[UInt8], offset:Int)->Float32{
    var pointer = UnsafePointer<UInt8>(bytes)
    pointer = pointer.advancedBy(offset)

    let fPointer =  UnsafePointer<Float32>(pointer)
    return fPointer.memory

}

#1


13  

Drop the & on &bytes. bytes is an array.

删除& on和bytes。字节是一个数组。

    var bytes:Array<UInt8> = [0x9A, 0x99, 0x99, 0x41] //19.20000

    var f:Float = 0.0

    memccpy(&f, bytes, 4, 4) // as per OP. memcpy(&f, bytes, 4) preferred

    println ("f=\(f)")// f=19.2000007629395

Update Swift 3

更新快3

memccpy does not seem to work in Swift 3. As commentators have said, use memcpy :

memccpy似乎在Swift 3中不起作用。正如评论人士所说,使用memcpy:

import Foundation
var bytes:Array<UInt8> = [0x9A, 0x99, 0x99, 0x41] //19.20000

var f:Float = 0.0

/* Not in Swift 3
 memccpy(&f, bytes, 4, 4) // as per OP.

 print("f=\(f)")// f=19.2
 */

memcpy(&f, bytes, 4) /

print("f=\(f)")// f=19.2

#2


2  

public func parseInt32(bytes:[UInt8], offset:Int)->Int32{

    var pointer = UnsafePointer<UInt8>(bytes)
    pointer = pointer.advancedBy(offset)

    let iPointer =  UnsafePointer<Int32>(pointer)
    return iPointer.memory

}

public func parseFloat32(bytes:[UInt8], offset:Int)->Float32{
    var pointer = UnsafePointer<UInt8>(bytes)
    pointer = pointer.advancedBy(offset)

    let fPointer =  UnsafePointer<Float32>(pointer)
    return fPointer.memory

}