34362

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

Question:

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.

Answer1:

Try this it may help you

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

Answer2:

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

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

Recommend

  • Filtering and Grouping data from table with key/value pairs
  • Filtering and Grouping data from table with key/value pairs
  • How to feed hour and minute data into chartJS
  • Estimating time until battery is depleted for Android phones programmatically
  • How read between delimiters in php DOM of a XML file?
  • Visualizing a large matrix in matlab
  • Change attribute of custom directive
  • What does certain JVM do after loading ByteCode into memory?
  • Should a web service response include empty values?
  • is there a way to update filter with async data
  • Changing Jupyter Notebook start up folder by modifying “start in” not working any more
  • Xmonad multiple submap key combos
  • Using Generics on right hand side in Java 6?
  • VBA Excel, loop through variables
  • How to use function wrapper in mustache.php?
  • How can I replace the server in Web Component Tester
  • Convert SQLite database to XML
  • How to assign byte[] as a pointer in C#
  • ListItem.Attributes.Add not working
  • WPF Visiblity Binding to Boolean Expression with multiple Variables
  • Conversion from string “a” to type 'Boolean' is not valid
  • MySQL Order by column = x, column asc?
  • Allowing both email and username for authentication
  • Get one-time binding to work for ng-if
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • Fetching methods from BroadcastReceiver to update UI
  • Can Jackson SerializationFeature be overridden per field or class?
  • Does CUDA 5 support STL or THRUST inside the device code?
  • How do you troubleshoot character encoding problems?
  • Apache 2.4 - remove | delete | uninstall
  • php design question - will a Helper help here?
  • Arrays break string types in Julia
  • Proper folder structure for lots of source files
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Django query for large number of relationships
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How do I use LINQ to get all the Items that have a particular SubItem?