Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split UInt32 into [UInt8] in swift

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?

like image 703
Kelo Avatar asked Apr 30 '15 14:04

Kelo


2 Answers

I had a similar need and was trying to learn a bit about manual memory management. Am I missing something, or has this gotten easier with newer versions of Swift? (I'm using Swift 5)

Creating an array of bytes ([UInt8]) from a UInt32

let example: UInt32 = 1

let byteArray = withUnsafeBytes(of: example.bigEndian) {
    Array($0)
}

print(byteArray) // [0, 0, 0, 1]

or if you want to append to an existing array, that can be done in the closure instead:

var existingArray: [UInt8] = [1, 2, 3]
let example: UInt32 = 1

withUnsafeBytes(of: example.bigEndian) {
    existingArray.append(contentsOf: $0)
}

print(existingArray)  // [1, 2, 3, 0, 0, 0, 1]
like image 191
user1325158 Avatar answered Sep 28 '22 17:09

user1325158


Another option is to extend the FixedWidthInteger protocol directly, so any UnsignedInteger automatically gets the function for free. This is how it looks building on @Benson's answer:

extension FixedWidthInteger where Self: UnsignedInteger {

    var bytes: [UInt8] {
        var _endian = littleEndian
        let bytePtr = withUnsafePointer(to: &_endian) {
            $0.withMemoryRebound(to: UInt8.self, capacity: MemoryLayout<Self>.size) {
                UnsafeBufferPointer(start: $0, count: MemoryLayout<Self>.size)
            }
        }
        return [UInt8](bytePtr)
    }

}

We can verify this with a unit test:

func test_bytes() {
    XCTAssertEqual(UInt8.min.bytes, [0])
    XCTAssertEqual(UInt8.max.bytes, [255])

    XCTAssertEqual(UInt16.min.bytes, [0, 0])
    XCTAssertEqual(UInt16.max.bytes, [255, 255])

    XCTAssertEqual(UInt32.min.bytes, [0, 0, 0, 0])
    XCTAssertEqual(UInt32.max.bytes, [255, 255, 255, 255])

    XCTAssertEqual(UInt64.min.bytes, [0, 0, 0, 0, 0, 0, 0, 0])
    XCTAssertEqual(UInt64.max.bytes, [255, 255, 255, 255, 255, 255, 255, 255])
}
like image 31
Kane Cheshire Avatar answered Sep 28 '22 16:09

Kane Cheshire