0

在下面的代码中testP.deviceIDtestP.deviceID1应该具有相同的值。但是 testP.deviceID1 包含 2(它应该包含 513)。因此,关于对齐,我不了解一些基本的东西吗?

let byteArray:[UInt8] = [ 0x01,0x02,23,24,25,0x01,0x02 ]
let testData = Data(bytes: byteArray )
struct testStruct {
    let deviceID            :UInt16 //0..1
    let byte0               :UInt8
    let byte1               :UInt8
    let byte2               :UInt8
    let deviceID1           :UInt16 //5..6
}

let testP = testData.withUnsafeBytes { (ptr: UnsafePointer<testStruct>) -> testStruct in
    return ptr.pointee
}

print("\(testP.deviceID):\(testP.byte0):\(testP.deviceID1)")
4

0 回答 0