ios - 如何从 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))
- commandType 是一个枚举,它有多个包含 HEX 值的案例
- messageData是一个ByteArray,这个函数的参数
- NWPFunction 是一个包含函数“forCommandId”的类,它将返回给定 16 位整数“ctValue”的特定值
- ++() 是我添加的前缀函数,因此它的编译
解决方案
推荐阅读
- java - 实现一个java本地库来flutter插件模板
- javascript - document.getElementById('blocks').value = blocks; 给出错误
- flutter - Flutter 更改 Scaffold 抽屉图标
- c++ - 为什么这个快速排序实现会给出一个奇怪的输出
- hash - 一次调用处理视频文件中的多个流
- java - resteasy 如何从 META-INF/services/javax.ws.rs.ext.Providers 文件中选择它的提供者?
- javascript - 如何在Javascript中获取一年中的一天
- java - 如何将房间数据库中的列表数据添加到android中的警报对话框
- python - 如何检查 OptionMenu 的选项是什么?
- search - 数字报纸图书馆的搜索引擎是如何工作的?