如何将数据转换为 UInt8 数组?
func serialPort(_ serialPort: ORSSerialPort, didReceive data: Data) {
print("recieved:\(data)")
let arr: [UInt8] = Data(???)???
}
收到的日志:70 字节
如何将数据转换为 UInt8 数组?
func serialPort(_ serialPort: ORSSerialPort, didReceive data: Data) {
print("recieved:\(data)")
let arr: [UInt8] = Data(???)???
}
收到的日志:70 字节
在 Swift 3 中,Data
作为 a Collection
of工作UInt8
,所以你可以简单地使用Array.init
.
var received: [UInt8] = []
func serialPort(_ serialPort: ORSSerialPort, didReceive data: Data) {
print("received:\(data))")
received = Array(data)
}
但是,(Array.init
或Array.append(contentsOf:)
)复制.Data
Data
知道了!
var recived = [UInt8]()
func serialPort(_ serialPort: ORSSerialPort, didReceive data: Data) {
recived.removeAll()
print("recieved:\(data))")
recived.append(contentsOf: data)
}
let data = "ABCD".data(using: .ascii)!
data.withUnsafeBytes { (pointer: UnsafePointer<UInt8>) in
//Prints 67 which is the ASCII value of 'C'
print(pointer[2])
}