这是我将字节数据转换为float的代码。我尝试了这个网站给出的每个答案。我得到了指数值"< 44fa0000>"字节数据
static func returnFloatValue(mutableData:NSMutableData)->Float
{
let qtyRange = mutableData.subdataWithRange(NSMakeRange(0, 4))
let qtyString = String(qtyRange)
let qtyTrimString = qtyString.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "<>"))
let qtyValue = Float(strtoul(qtyTrimString, nil, 16)/10)
return qtyValue
}
谢谢
答案 0 :(得分:11)
<44fa0000>
是 big-endian 的内存表示形式
二进制浮点数2000.0
。从中获取号码
数据,您必须先将其读入UInt32
,然后再转换
big-endian来托管byteorder,然后将结果转换为
Float
。
在 Swift 2 中
func floatValueFromData(data: NSData) -> Float {
return unsafeBitCast(UInt32(bigEndian: UnsafePointer(data.bytes).memory), Float.self)
}
示例:
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
在 Swift 3 中,您可以使用Data
代替NSData
,
unsafeBitCast
可以替换为Float(bitPattern:)
初始化:
func floatValue(data: Data) -> Float {
return Float(bitPattern: UInt32(bigEndian: data.withUnsafeBytes { $0.pointee } ))
}
答案 1 :(得分:0)
使用此功能:
<CircularProgress size={2} style={Styles.mainLoader}/>
mainLoader: {
position: 'absolute',
paddingTop: '15%',
width: '100%',
height: '100%',
zIndex: 1000,
backgroundColor: '#000000',
opacity: 0.5,
textAlign: 'center',
}
你可以在这里看到它:
static func returnFloatValue(data: NSMutableData) -> Float {
let bytes = [UInt8](data as Data)
var f: Float = 0
memcpy(&f, bytes, 4)
return f
}
答案 2 :(得分:0)
这是一些 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) }
警告:如果您的数据小于4个字节,则会抛出此示例。
。
安全数据扩展名:
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) }
}
}
}
测试:
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())")
您可能会找到一种更快的方法,但这足以满足我的需求