Nithinjith
Nithinjith

Reputation: 1825

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.

Upvotes: 0

Views: 2207

Answers (2)

Jeyanth Inba
Jeyanth Inba

Reputation: 23

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

-TESTED- This is the general form for both positive and negative numbers

Upvotes: 0

Dixit Panchal
Dixit Panchal

Reputation: 3436

Try this it may help you

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

Upvotes: 2

Related Questions