The code which I am using for getting data in bytes, but I am not getting correct byte data for float value
let count = data.length / sizeof(UInt32)
// create array of appropriate length:
var array = [UInt32](count: count, repeatedValue: 0)
// copy bytes into array
data.getBytes(&array, length:count * sizeof(UInt32))
print(array)
Float
to NSData
:
var float1 : Float = 40.0
let data = NSData(bytes: &float1, length: sizeofValue(float1))
print(data) // <00002042>
... and back to Float
:
var float2 : Float = 0
data.getBytes(&float2, length: sizeofValue(float2))
print(float2) // 40.0
(The same would work for other "simple" types like Double
,
Int
, ...)
Update for Swift 3, using the new Data
type:
var float1 : Float = 40.0
let data = Data(buffer: UnsafeBufferPointer(start: &float1, count: 1))
print(data as NSData) // <00002042>
let float2 = data.withUnsafeBytes { $0.pointee } as Float
print(float2) // 40.0
(See also round trip Swift number types to/from Data)
Update for Swift 4 and later:
var float1 : Float = 40.0
let data = Data(buffer: UnsafeBufferPointer(start: &float1, count: 1))
let float2 = data.withUnsafeBytes { $0.load(as: Float.self) }
print(float2) // 40.0
Remark: load(as:)
requires the data to be properly aligned, for Float
that would be on a 4 byte boundary. See e.g. round trip Swift number types to/from Data for other solutions which work for arbitrarily aligned data.
Float(bitPattern: UInt32(bigEndian: data.withUnsafeBytes { $0.pointee } ))
whats different in this data to float conversion as it's outputting different value actually I need reverse of this means Float to Data but above code in your answer is not converting as expected, please helpNote that the Swift 3 version shown here now generates a warning with Swift 5:
Initialization of 'UnsafeBufferPointer<Self>' results in a dangling buffer pointer
@featherless: I get a different warning “'withUnsafeBytes' is deprecated” and I have updated the code. It should now compile without warnings.
Thanks for the update Martin. The new code does work + compile but it looks like it doesn't handle misaligned data. See github.com/jverkoey/BinaryCodable/pull/58/files for the fix I put in place based on swift-nio's number conversion implementation: github.com/apple/swift-nio/blob/…
@featherless: That is correct,
load(as:)
requires the data to be properly aligned. (See also forums.swift.org/t/built-in-unaligned-loads/19664 in the Swift Forum.) For arbitrarily aligned data I wrote something here stackoverflow.com/a/38024025/1187415, that is essentially the same approach as in your link.