How to convert bytes to a float value in swift?

后端 未结 3 1677
悲哀的现实
悲哀的现实 2020-12-16 22:54

This is my code to convert byte data to float. I tried every answers given in this site. I am getting exponential value for this \"<44fa0000>\" byte data



        
相关标签:
3条回答
  • 2020-12-16 23:27

    Use this function:

    static func returnFloatValue(data: NSMutableData) -> Float {
        let bytes = [UInt8](data as Data)
        var f: Float = 0
    
        memcpy(&f, bytes, 4)
        return f
    }
    

    And you can see it in action here:

    var initialValue: Float = 19.200
    
    let data = NSMutableData(bytes: &initialValue, length: 4)
    
    func returnFloatValue(data: NSMutableData) -> Float {
        let bytes = [UInt8](data as Data)
        var f: Float = 0
    
        memcpy(&f, bytes, 4)
        return f
    }
    
    var result:Float = returnFloatValue(data: data)
    
    print("f=\(result)")// f=19.2
    
    0 讨论(0)
  • 2020-12-16 23:46

    Here is some swift 5:

    let data = Data([0x44, 0xfa, 0x00, 0x00]) // 0x44fa0000
    let floatNb:Float = data.withUnsafeBytes { $0.load(as: Float.self) }
    
    // note that depending on the input endianess, you could add .reversed() to data
    let floatNb:Float = data.reversed().withUnsafeBytes { $0.load(as: Float.self) }
    

    WARNING: this sample throws if your Data is under 4 bytes..

    .

    Safe Data extension:

    extension Data {
        enum Endianess {
            case little
            case big
        }
    
        func toFloat(endianess: Endianess = .little) -> Float? {
            guard self.count <= 4 else { return nil }
    
            switch endianess {
            case .big:
                let data = [UInt8](repeating: 0x00, count: 4-self.count) + self
                return data.withUnsafeBytes { $0.load(as: Float.self) }
            case .little:
                let data = self + [UInt8](repeating: 0x00, count: 4-self.count)
                return data.reversed().withUnsafeBytes { $0.load(as: Float.self) }
            }
        }
    }
    

    Tests:

    let opData        = Data([0x44, 0xFA, 0x00, 0x00])
    let nb42          = Data([0x42, 0x28])
    let nb42bigEndian = Data([0x28, 0x42])
    let tooBig        = Data([0x44, 0xFA, 0x00, 0x00, 0x00])
    
    print("opData:        \(opData.toFloat())")
    print("nb42:          \(nb42.toFloat())")
    print("nb42bigEndian: \(nb42bigEndian.toFloat(endianess: .big))")
    print("tooBig:        \(tooBig.toFloat())")
    

    you may find a faster way but this was good enough for my needs

    0 讨论(0)
  • 2020-12-16 23:49

    <44fa0000> is the big-endian memory representation of the binary floating point number 2000.0. To get the number back from the data, you have to read it into an UInt32 first, convert from big-endian to host byteorder, and then cast the result to a Float.

    In Swift 2 that would be

    func floatValueFromData(data: NSData) -> Float {
        return unsafeBitCast(UInt32(bigEndian: UnsafePointer(data.bytes).memory), Float.self)
    }
    

    Example:

    let bytes: [UInt8] =  [0x44, 0xFA, 0x00, 0x00]
    let data = NSData(bytes: bytes, length: 4)
    
    print(data) // <44fa0000>
    let f = floatValueFromData(data)
    print(f) // 2000.0
    

    In Swift 3 you would use Data instead of NSData, and the unsafeBitCast can be replaced by the Float(bitPattern:) initializer:

    func floatValue(data: Data) -> Float {
        return Float(bitPattern: UInt32(bigEndian: data.withUnsafeBytes { $0.pointee } ))
    }
    

    In Swift 5 the withUnsafeBytes() method of Data calls the closure with an (untyped) UnsafeRawBufferPointer, and you can load() the value from the raw memory:

    func floatValue(data: Data) -> Float {
        return Float(bitPattern: UInt32(bigEndian: data.withUnsafeBytes { $0.load(as: UInt32.self) }))
    }
    
    0 讨论(0)
提交回复
热议问题