2 // Int+OAuthSwift.swift
5 // Created by Dongri Jin on 1/28/15.
6 // Copyright (c) 2015 Dongri Jin. All rights reserved.
12 public func bytes(_ totalBytes: Int = MemoryLayout<Int>.size) -> [UInt8] {
13 return arrayOfBytes(self, length: totalBytes)
17 private func arrayOfBytes<T>(_ value: T, length: Int? = nil) -> [UInt8] {
18 let totalBytes = length ?? MemoryLayout<T>.size
20 let valuePointer = UnsafeMutablePointer<T>.allocate(capacity: 1)
21 valuePointer.pointee = value
23 let bytesPointer = UnsafeMutablePointer<UInt8>(OpaquePointer(valuePointer))
24 var bytes = [UInt8](repeating: 0, count: totalBytes)
25 for j in 0..<min(MemoryLayout<T>.size, totalBytes) {
26 bytes[totalBytes - 1 - j] = (bytesPointer + j).pointee
29 valuePointer.deinitialize(count: 1)
30 valuePointer.deallocate()