Created
August 28, 2019 09:39
-
-
Save wotjd/bac25863ef16d06fd96ecba5cef7e509 to your computer and use it in GitHub Desktop.
combine byte array using reduce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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