Skip to content

Instantly share code, notes, and snippets.

@bpolania
Last active January 25, 2024 07:10
Show Gist options
  • Star 62 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save bpolania/704901156020944d3e20fef515e73d61 to your computer and use it in GitHub Desktop.
Save bpolania/704901156020944d3e20fef515e73d61 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)
]
}
}
@Stay-in-Touch
Copy link

Hi Boris.
This code is very helpful. However it gives an error in Swift5.

'withUnsafeBytes' is deprecated: use withUnsafeBytes<R>(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R instead

in the function var uint16: UInt16 {
get {
let i16array = self.withUnsafeBytes {
UnsafeBufferPointer(start: $0, count: self.count/2).map(UInt16.init(littleEndian:))
}
return i16array[0]
}
}

I appreciate very much your effort to hide away this ugly construct to give the programmer a natural view of Data.

John Brohan

@bpolania
Copy link
Author

bpolania commented Nov 25, 2019 via email

Copy link

ghost commented Nov 9, 2021

Did this get updated for swift 5.5? It still has withUsafeBytes which is deprecated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment