简体   繁体   English

Kotlin Android Studio如何将两个uint8合并为一个uint16

[英]Kotlin Android Studio how to merge two uint8 to one uint16

I am reading data from bluetooth gatt characteristic.我正在从蓝牙 gatt 特性中读取数据。 First data is one byte and I am successfully reading it by code:第一个数据是一个字节,我通过代码成功读取了它:

val strValue = characteristic.value[0].toUByte()

characteristic.value[1] contains most significant byte of uint16 characteristic.value[1] 包含 uint16 的最高有效字节

characteristic.value[2] contains least significant byte of uint16 characteristic.value[2] 包含 uint16 的最低有效字节

What I want to do is get uint16 and put it into strValue.我想要做的是获取 uint16 并将其放入 strValue。

I've tried to use shl function but it brings me this error: IMAGE1我试过使用 shl function 但它给我带来了这个错误: IMAGE1

I also tried this: IMAGE2我也试过这个: IMAGE2

How to proper do this in Kotlin?如何在 Kotlin 中正确执行此操作? I am good in C but Kotlin is new for me.我擅长 C,但 Kotlin 对我来说是新的。

Solution:解决方案:

val strValue :UShort = (characteristic.getIntValue(FORMAT_UINT16,1) or characteristic.getIntValue(FORMAT_UINT16,2).shl(8)).toUShort()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM