首页 > 解决方案 > 如何从 ByteArray 数组中转换 2 个值到 Swift 4 中的 16 位整数 UInt16?

问题描述

使用 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))

标签: iosarraysswiftuint16

解决方案


推荐阅读