Created
February 26, 2015 12:13
-
-
Save cxa/48f1be2653fd35843d98 to your computer and use it in GitHub Desktop.
Convert an Integer to bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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