1

Using Xcode 9.4.1 and Swift 4.1

I have a function which reads an Array as Input parameter (see messageData in my Code). Now I want to assign 2 specific Bytes (so 2 values of the ByteArray) to one single UInt16. So I saw the approach with the 0xff in more questions, but it doesn't work for me. The 16-Bit Integer I want to convert is 28281.

    var ctValue : UInt16 = CFSwapInt16HostToBig(UInt16(commandType!.rawValue))
    let ctValueData: UnsafeMutablePointer<UInt16> = UnsafeMutablePointer<UInt16>(&ctValue)
    ctValueData[1] = UInt16(CChar(((messageData[++(idx)]) >> 8) & 0xff))
    ctValueData[0] = UInt16(CChar(((messageData[++(idx)]) >> 0) & 0xff))
    commandType = NWPFunction.forCommandId(commandId:Int(ctValue))
  • commandType is an Enum which has multiple cases containing HEX-Values
  • messageData is a ByteArray, the parameter of this function
  • NWPFunction is a class containing a function "forCommandId" which will return a specific value for the given 16-Bit Integer "ctValue"
  • the ++() is a prefix function which I added so its compiling
leachim
  • 167
  • 4
  • 12

0 Answers0