Skip to content

Instantly share code, notes, and snippets.

@cxa
Created February 26, 2015 12:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cxa/48f1be2653fd35843d98 to your computer and use it in GitHub Desktop.
Save cxa/48f1be2653fd35843d98 to your computer and use it in GitHub Desktop.
Convert an Integer to bytes
func bytes<T: IntegerType>(i: T) -> [UInt8] {
let p = UnsafeMutablePointer<T>.alloc(1)
p.memory = i
let b = unsafeBitCast(p, UnsafePointer<UInt8>.self)
let bytes = reduce(0..<sizeof(T), []) { $0 + [b[$1]] }
p.destroy()
return bytes
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment