Skip to content

Instantly share code, notes, and snippets.

@tmspzz
Created April 14, 2020 09:07
Show Gist options
  • Save tmspzz/cdaa37121a9b38942879fb93e05efd79 to your computer and use it in GitHub Desktop.
Save tmspzz/cdaa37121a9b38942879fb93e05efd79 to your computer and use it in GitHub Desktop.
UUID from and to two UInt64
extension UUID {
init(numbers: (UInt64, UInt64)) {
var firstNumber = numbers.0
var secondNumber = numbers.1
let firstData = Data(bytes: &firstNumber, count: MemoryLayout<UInt64>.size)
let secondData = Data(bytes: &secondNumber, count: MemoryLayout<UInt64>.size)
let bytes = [UInt8](firstData) + [UInt8](secondData)
let tuple: uuid_t = (bytes[0], bytes[1], bytes[2], bytes[3],
bytes[4], bytes[5], bytes[6], bytes[7],
bytes[8], bytes[9], bytes[10], bytes[11],
bytes[12], bytes[13], bytes[14], bytes[15])
self.init(uuid: tuple)
}
var intTupleValue: (lower: UInt64, upper: UInt64) {
let tuple = self.uuid
let firstBytes: [UInt8] = [tuple.0, tuple.1, tuple.2, tuple.3,
tuple.4, tuple.5, tuple.6, tuple.7]
let secondBytes: [UInt8] = [tuple.8, tuple.9, tuple.10, tuple.11,
tuple.12, tuple.13, tuple.14, tuple.15]
var firstData = Data(firstBytes)
var secondData = Data(secondBytes)
let first = withUnsafeBytes(of: &firstData) {
$0.load(as: UInt64.self)
}
let second = withUnsafeBytes(of: &secondData) {
$0.load(as: UInt64.self)
}
return (first, second)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment