34 lines
905 B
Swift
34 lines
905 B
Swift
//
|
|
// 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<Int>.size) -> [UInt8] {
|
|
return arrayOfBytes(self, length: totalBytes)
|
|
}
|
|
}
|
|
|
|
private func arrayOfBytes<T>(_ value: T, length: Int? = nil) -> [UInt8] {
|
|
let totalBytes = length ?? MemoryLayout<T>.size
|
|
|
|
let valuePointer = UnsafeMutablePointer<T>.allocate(capacity: 1)
|
|
valuePointer.pointee = value
|
|
|
|
let bytesPointer = UnsafeMutablePointer<UInt8>(OpaquePointer(valuePointer))
|
|
var bytes = [UInt8](repeating: 0, count: totalBytes)
|
|
for j in 0..<min(MemoryLayout<T>.size, totalBytes) {
|
|
bytes[totalBytes - 1 - j] = (bytesPointer + j).pointee
|
|
}
|
|
|
|
valuePointer.deinitialize(count: 1)
|
|
valuePointer.deallocate()
|
|
|
|
return bytes
|
|
}
|