如何在Swift 3中将UInt16转换为UInt8?
我想将UInt16转换为UInt8数组,但收到以下错误消息:
I want to convert UInt16 to UInt8 array, but am getting the following error message:
'init'不可用:使用'withMemoryRebound(to:capacity:_)' 暂时将内存视为另一种与布局兼容的类型.
'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.
代码:
let statusByte: UInt8 = UInt8(status)
let lenghtByte: UInt16 = UInt16(passwordBytes.count)
var bigEndian = lenghtByte.bigEndian
let bytePtr = withUnsafePointer(to: &bigEndian) {
UnsafeBufferPointer<UInt8>(start: UnsafePointer($0), count: MemoryLayout.size(ofValue: bigEndian))
}
如错误消息所示,您必须使用withMemoryRebound()
将UInt16
的指针重新解释为UInt8
的指针:
As the error message indicates, you have to use withMemoryRebound()
to reinterpret the pointer to UInt16
as a pointer to UInt8
:
let bytes = withUnsafePointer(to: &bigEndian) {
$0.withMemoryRebound(to: UInt8.self, capacity: MemoryLayout.size(ofValue: bigEndian)) {
Array(UnsafeBufferPointer(start: $0, count: MemoryLayout.size(ofValue: bigEndian)))
}
}
使用仅有效的指针($0
)调用闭包
在关闭的整个生命周期内,不得传递到外部
供以后使用.这就是创建Array
并将其用作返回值的原因.
The closures are invoked with pointers ($0
) which are only valid
for the lifetime of the closure and must not be passed to the outside
for later use. That's why an Array
is created and used as return value.
但是有一个更简单的解决方案:
There is a simpler solution however:
let bytes = withUnsafeBytes(of: &bigEndian) { Array($0) }
说明: withUnsafeBytes
用UnsafeRawBufferPointer
调用闭包来存储bigEndian
变量.
由于UnsafeRawBufferPointer
是UInt8
的Sequence
,因此需要一个数组
可以使用Array($0)
从中创建.
Explanation: withUnsafeBytes
invokes the closure with a UnsafeRawBufferPointer
to the storage of the bigEndian
variable.
Since UnsafeRawBufferPointer
is a Sequence
of UInt8
, an array
can be created from that with Array($0)
.