mirror of
https://github.com/tsolomko/SWCompression.git
synced 2026-06-23 14:56:41 +00:00
49 lines
912 B
Swift
49 lines
912 B
Swift
//
|
|
// DataExtension.swift
|
|
// SWCompression
|
|
//
|
|
// Created by Timofey Solomko on 19.09.16.
|
|
// Copyright © 2016 Timofey Solomko. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
extension Data {
|
|
|
|
func to<T>(type: T.Type) -> T {
|
|
return self.withUnsafeBytes { $0.pointee }
|
|
}
|
|
|
|
func toArray<T>(type: T.Type) -> [T] {
|
|
return self.withUnsafeBytes {
|
|
[T](UnsafeBufferPointer(start: $0, count: self.count/MemoryLayout<T>.size))
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
extension UInt8 {
|
|
|
|
func toInt() -> Int {
|
|
return Int(bitPattern: UInt(self))
|
|
}
|
|
|
|
}
|
|
|
|
extension Int {
|
|
|
|
func toUInt8() -> UInt8 {
|
|
return UInt8(truncatingBitPattern: UInt(self))
|
|
}
|
|
|
|
func reverseBytes() -> Int {
|
|
var result = 0
|
|
for i in 0..<4 {
|
|
let byte = ((self & (0xFF << (i * 8))) >> (i * 8))
|
|
result += byte << (8 * (3 - i))
|
|
}
|
|
return result
|
|
}
|
|
|
|
}
|