0

The Swift library includes the function bigEndian that can be used on integer types (such as Int, UInt, UInt8, UInt64, Int64, etc) to convert them from host order (which might presumably be anything, but realistically will be big or little endian) to network byte order (which is big endian). There're some good SO answers referring to this, and a particularly complete one is here.

However, I've not found a good resource that covers arranging a Float (32 bit) or Double (64 bit) type in to network byte order. Given that these types don't have a bigEndian method, I'm wondering if there is some subtlety involved? (The linked question does discuss floating point types, but I'm not sure it is definitely covering all details that might be relevant).

Specifically, I want to handle the 64 bit Double floating point type. I'd like a solution that will work on any platform where Swift is available.

Thank you.

Benjohn
  • 13,228
  • 9
  • 65
  • 127
  • It seems like `bitPattern` on a `Double` [does this](https://developer.apple.com/documentation/swift/double/1847031-init) and provides the data in the "IEEE 754 specification". So a solution might be: `withUnsafeBytes(of: double.bitPattern) { self.data.append(contentsOf: $0) }`? – Benjohn Sep 17 '20 at 19:17
  • Network byte order is big endian, so this might be what you are looking for: https://stackoverflow.com/a/56167933/1187415 – Martin R Sep 17 '20 at 19:30

0 Answers0