1

使用 Xcode 9.4.1 和 Swift 4.1

我有一个函数,它读取一个数组作为输入参数(参见我的代码中的 messageData)。现在我想将 2 个特定字节(即 ByteArray 的 2 个值)分配给一个 UInt16。所以我看到了0xff更多问题的方法,但它对我不起作用。我要转换的 16 位整数是 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 是一个枚举,它有多个包含 HEX 值的案例
  • messageData是一个ByteArray,这个函数的参数
  • NWPFunction 是一个包含函数“forCommandId”的类,它将返回给定 16 位整数“ctValue”的特定值
  • ++() 是我添加的前缀函数,因此它的编译
4

0 回答 0