Skip to content

Instantly share code, notes, and snippets.

@wotjd
Created August 28, 2019 09:39
Show Gist options
  • Save wotjd/bac25863ef16d06fd96ecba5cef7e509 to your computer and use it in GitHub Desktop.
Save wotjd/bac25863ef16d06fd96ecba5cef7e509 to your computer and use it in GitHub Desktop.
combine byte array using reduce
import Foundation
extension ExpressibleByIntegerLiteral {
var bytes: [UInt8] { return withUnsafeBytes(of: self) { Array($0) } }
}
var id = UInt8(exactly: 1)!
var count = UInt8(exactly: 2)!
var timeStart = UInt64(exactly: 0x1234567898765432)!
var timeEnd = UInt64(exactly: 4)!
var size = UInt32(exactly: 5)!
let packet = [timeStart.bigEndian.bytes, timeEnd.bigEndian.bytes, size.bigEndian.bytes].reduce([id, count], +)
print(packet)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment