Skip to content

Instantly share code, notes, and snippets.

@zinwalin
Forked from bpolania/DataExtensions.swift
Created April 27, 2022 08:32
Show Gist options
  • Save zinwalin/9bd009073d7a63e62b08804481bc1564 to your computer and use it in GitHub Desktop.
Save zinwalin/9bd009073d7a63e62b08804481bc1564 to your computer and use it in GitHub Desktop.
Swift Extensions for Data, Int, UInt8, UInt16, and UInt32 types
// MIT License
// Copyright (c) 2018 Boris Polania
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// DataExtensions.swift
//
// Created by Boris Polania on 2/16/18.
//
import UIKit
extension Data {
var uint8: UInt8 {
get {
var number: UInt8 = 0
self.copyBytes(to:&number, count: MemoryLayout<UInt8>.size)
return number
}
}
var uint16: UInt16 {
get {
let i16array = self.withUnsafeBytes { $0.load(as: UInt16.self) }
return i16array
}
}
var uint32: UInt32 {
get {
let i32array = self.withUnsafeBytes { $0.load(as: UInt32.self) }
return i32array
}
}
var uuid: NSUUID? {
get {
var bytes = [UInt8](repeating: 0, count: self.count)
self.copyBytes(to:&bytes, count: self.count * MemoryLayout<UInt32>.size)
return NSUUID(uuidBytes: bytes)
}
}
var stringASCII: String? {
get {
return NSString(data: self, encoding: String.Encoding.ascii.rawValue) as String?
}
}
var stringUTF8: String? {
get {
return NSString(data: self, encoding: String.Encoding.utf8.rawValue) as String?
}
}
struct HexEncodingOptions: OptionSet {
let rawValue: Int
static let upperCase = HexEncodingOptions(rawValue: 1 << 0)
}
func hexEncodedString(options: HexEncodingOptions = []) -> String {
let format = options.contains(.upperCase) ? "%02hhX" : "%02hhx"
return map { String(format: format, $0) }.joined()
}
}
// MIT License
// Copyright (c) 2018 Boris Polania
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// DataExtensions.swift
//
// Created by Boris Polania on 2/16/18.
//
//
// IntsExtensions.swift
//
// Created by Boris Polania on 2/19/18.
//
import UIKit
extension Int {
var data: Data {
var int = self
return Data(bytes: &int, count: MemoryLayout<Int>.size)
}
}
extension UInt8 {
var data: Data {
var int = self
return Data(bytes: &int, count: MemoryLayout<UInt8>.size)
}
}
extension UInt16 {
var data: Data {
var int = self
return Data(bytes: &int, count: MemoryLayout<UInt16>.size)
}
}
extension UInt32 {
var data: Data {
var int = self
return Data(bytes: &int, count: MemoryLayout<UInt32>.size)
}
var byteArrayLittleEndian: [UInt8] {
return [
UInt8((self & 0xFF000000) >> 24),
UInt8((self & 0x00FF0000) >> 16),
UInt8((self & 0x0000FF00) >> 8),
UInt8(self & 0x000000FF)
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment