【问题标题】:How to convert byte position in swift如何快速转换字节位置
【发布时间】:2019-05-30 20:21:12
【问题描述】:

如何正确转换它?我需要的值是字节数 0-1,格式为 uint16,单位为度。

print("derived : \(characteristic.value!)")
print(String(bytes: characteristic.value!, encoding: .utf16))

derived : 20 bytes
Optional("\0{Ͽ⌜ƀ")

【问题讨论】:

标签: swift byte


【解决方案1】:

您只需要将Data 的前两个字节设为UInt16

var result: UInt16 = 0
_ = withUnsafeMutableBytes(of: &result) {characteristic.value!.copyBytes(to: $0, from: 0...1)}
print("\(result) degrees")

假设您的 uint16 格式 是 Little-Endian 以及 iOS,这在 BLE 特性中更常见。

(当您在Data 中获得第一个值时,不需要from: 0...1,但您可能需要其他位置的一些数据。)

【讨论】:

  • 跑了0度
  • @DisplayName,然后您的characteristic.value0 保持在字节数0-1
猜你喜欢
  • 2021-06-04
  • 2013-12-14
  • 1970-01-01
  • 2017-05-21
  • 1970-01-01
  • 2016-08-17
  • 1970-01-01
  • 2021-08-23
相关资源
最近更新 更多