Split UInt32 into [UInt8] in swift

只谈情不闲聊 提交于 2019-12-19 06:22:15

问题


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?


回答1:


Your loop can more compactly be written as

let byteArray = 24.stride(through: 0, by: -8).map {
    UInt8(truncatingBitPattern: example >> UInt32($0))
}

Alternatively, create an UnsafeBufferPointer and convert that to an array:

let example: UInt32 = 72 << 24 | 66 << 16 | 1 << 8 | 15

var bigEndian = example.bigEndian
let bytePtr = withUnsafePointer(&bigEndian) {
    UnsafeBufferPointer<UInt8>(start: UnsafePointer($0), count: sizeofValue(bigEndian))
}
let byteArray = Array(bytePtr)

print(byteArray) // [72, 66, 1, 15]

Update for Swift 3 (Xcode 8 beta 6):

var bigEndian = example.bigEndian
let count = MemoryLayout<UInt32>.size
let bytePtr = withUnsafePointer(to: &bigEndian) {
    $0.withMemoryRebound(to: UInt8.self, capacity: count) {
        UnsafeBufferPointer(start: $0, count: count)
    }
}
let byteArray = Array(bytePtr)



回答2:


Improved @Martin R's answered. Worked on UInt16, UInt32, and UInt64:

protocol UIntToBytesConvertable {
    var toBytes: [Byte] { get }
}

extension UIntToBytesConvertable {
    func toByteArr<T: Integer>(endian: T, count: Int) -> [Byte] {
        var _endian = endian
        let bytePtr = withUnsafePointer(to: &_endian) {
            $0.withMemoryRebound(to: Byte.self, capacity: count) {
                UnsafeBufferPointer(start: $0, count: count)
            }
        }
        return [Byte](bytePtr)
    }
}

extension UInt16: UIntToBytesConvertable {
    var toBytes: [Byte] {
        return toByteArr(endian: self.littleEndian,
                         count: MemoryLayout<UInt16>.size)
    }
}

extension UInt32: UIntToBytesConvertable {
    var toBytes: [Byte] {
        return toByteArr(endian: self.littleEndian,
                         count: MemoryLayout<UInt32>.size)
    }
}

extension UInt64: UIntToBytesConvertable {
    var toBytes: [Byte] {
        return toByteArr(endian: self.littleEndian,
                         count: MemoryLayout<UInt64>.size)
    }
}



回答3:


You can cast from one UnsafeMutablePointer type to another:

var arr = UnsafeMutablePointer<UInt32>.alloc(1)
arr.memory = example
var arr2 = UnsafeMutablePointer<UInt8>(arr)



回答4:


I have a similar problem with Xcode 8 Beta 6: write this line

var value = String(cString: sqlite3_column_text(stmt, index))

   to


let txt = UnsafePointer<Int8>(sqlite3_column_text(stmt, index))

and solve problems




回答5:


Improved @Martin R's answered.

func toByteArrary<T>(value: T)  -> [UInt8] where T: UnsignedInteger, T: FixedWidthInteger{
  var bigEndian = value.bigEndian
  let count = MemoryLayout<T>.size
  let bytePtr = withUnsafePointer(to: &bigEndian) {
    $0.withMemoryRebound(to: UInt8.self, capacity: count) {
        UnsafeBufferPointer(start: $0, count: count)
    }
  }

  return Array(bytePtr)
}



回答6:


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])
}


来源:https://stackoverflow.com/questions/29970204/split-uint32-into-uint8-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!