0

In the following code testP.deviceID and testP.deviceID1 should have the same value. However testP.deviceID1 contains 2 (it should contain 513). So there is something fundamental that I am not understanding regarding alignment?

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)")
eklektek
  • 1,083
  • 1
  • 16
  • 31

0 Answers0