I want to add UInt32
to byte buffer for which I use [UInt8]
. In java, there is convenient ByteBuffer class that has methods like putInt() for cases exactly like this. How could this be done in swift?
I guess I could solve this as following:
let example: UInt32 = 72 << 24 | 66 << 16 | 1 << 8 | 15
var byteArray = [UInt8](count: 4, repeatedValue: 0)
for i in 0...3 {
byteArray[i] = UInt8(0x0000FF & example >> UInt32((3 - i) * 8))
}
This is quite verbose though, any simpler way?