简体   繁体   中英

How to Convert two 8 bit represented byte to single 16 bit represented integer value in Android

I have an Android application with the BLE module. the BLE device is giving byte array with 24 bytes. Each byte has a separate meaning. In the byte array, 10 and 11 items are voltage and its a combination of 16-bit representation.

eg: I am getting the 11 item as 0 and 12 item as 3. So I want to convert it to a single 16-bit representation value. Also, I want to get this as a float value, because I need to display the voltage as a float value in the UI. I don't know anyone already ask this question. If anyone know the formulae for 8 bit to 16-bit representation please add the formulae.

试试这个可能对您有帮助

short yourinteger16 = (short)(((bytes[0] & 0xFF) << 8) | (bytes[1] & 0xFF));
short yourinteger16 = ((short) ((bytes[0] & 0xff) | (bytes[1] << 8)));

-TESTED-这是正数和负数的通用形式

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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