将 Int 转换为 swift 中的 UInt8 数组

Convert Int to Array of UInt8 in swift

我想转换 swift 中 UInt8 列表中的标准整数。

var x:Int = 2019

2019 可以(例如)用十六进制 7E3 编写,所以我想要某种将 is 转换为 UInt8 列表的函数,如下所示。

var y:[Uint8] = [0x07, 0xE3]

我已经找到了这个: 但是 he/she 是转换数字的 ascii 符号而不是数字本身。所以他的例子 94887253 应该给出一个像 [0x05, 0xA7, 0xDD, 0x55].

这样的列表

在最好的情况下,我正在寻找的函数有某种用途,这样我也可以选择结果数组的最小长度,例如

foo(42, length:2) -> [0x00, 0x2A]

foo(42, length:4) -> [0x00, 0x00, 0x00, 0x2A]

你可以这样做:

let x: Int = 2019
let length: Int = 2 * MemoryLayout<UInt8>.size  //You could specify the desired length

let a = withUnsafeBytes(of: x) { bytes in
    Array(bytes.prefix(length))
}

let result = Array(a.reversed()) //[7, 227]

或者更一般地说,我们可以使用这个snippet的修改版本:

func bytes<U: FixedWidthInteger,V: FixedWidthInteger>(
    of value    : U,
    to type     : V.Type,
    droppingZeros: Bool
    ) -> [V]{

    let sizeInput = MemoryLayout<U>.size
    let sizeOutput = MemoryLayout<V>.size

    precondition(sizeInput >= sizeOutput, "The input memory size should be greater than the output memory size")

    var value = value
    let a =  withUnsafePointer(to: &value, {
        [=11=].withMemoryRebound(
            to: V.self,
            capacity: sizeInput,
            {
                Array(UnsafeBufferPointer(start: [=11=], count: sizeInput/sizeOutput))
        })
    })

    let lastNonZeroIndex =
        (droppingZeros ? a.lastIndex { [=11=] != 0 } : a.indices.last) ?? a.startIndex

    return Array(a[...lastNonZeroIndex].reversed())
}

let x: Int = 2019
bytes(of: x, to: UInt8.self, droppingZeros: true)   // [7, 227]
bytes(of: x, to: UInt8.self, droppingZeros: false)  // [0, 0, 0, 0, 0, 0, 7, 227]