// // Int+OAuthSwift.swift // OAuthSwift // // Created by Dongri Jin on 1/28/15. // Copyright (c) 2015 Dongri Jin. All rights reserved. // import Foundation extension Int { public func bytes(_ totalBytes: Int = MemoryLayout.size) -> [UInt8] { return arrayOfBytes(self, length: totalBytes) } } private func arrayOfBytes(_ value: T, length: Int? = nil) -> [UInt8] { let totalBytes = length ?? MemoryLayout.size let valuePointer = UnsafeMutablePointer.allocate(capacity: 1) valuePointer.pointee = value let bytesPointer = UnsafeMutablePointer(OpaquePointer(valuePointer)) var bytes = [UInt8](repeating: 0, count: totalBytes) for j in 0...size, totalBytes) { bytes[totalBytes - 1 - j] = (bytesPointer + j).pointee } valuePointer.deinitialize(count: 1) valuePointer.deallocate() return bytes }