Skip to content

Instantly share code, notes, and snippets.

@key
Last active December 21, 2016 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save key/56972ab53b7ff9bf42be to your computer and use it in GitHub Desktop.
Save key/56972ab53b7ff9bf42be to your computer and use it in GitHub Desktop.
Convert `Double` value to `NSData` in Swift
var x: Double = 0.99043125417
var length = sizeof(Double) // -> 8
var x_data = NSData(bytes: &x, length: length)
var buffer = [UInt8](count: sizeof(Double), repeatedValue: 0x00)
x_data.getBytes(&buffer, length: buffer.count)
print(buffer) // -> "[210, 21, 179, 226, 156, 177, 239, 63]\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment