-1

The datasheet put me 2 byte for a data, which is defined as :

"These two byte is a 16-bit value in 2's complement form, whose range is from 0xF800 (-4096) to 0x07FF (4095)"

I don't really understand how to parse this data in javascript.

Little squirrel
  • 29
  • 2
  • 11
  • 2
    The data sheet is wrong - `0xf800...0x07ff` is actually `-2048...2047` – Alnitak Apr 24 '17 at 08:43
  • 1
    notwithstanding the error, to answer your question we'd need to now how this data is arriving in your application, and how it's currently stored. – Alnitak Apr 24 '17 at 08:49
  • Yes, this is what i thought too. As i know, values weren't good. I wiil call them. Well, my data is arriving in raw data that i parse in uint8Array. Maybe i have to change the type of these 2 bytes to have the value – Little squirrel Apr 24 '17 at 08:56

1 Answers1

0

All current mainstream browsers support the DataView class.

Given your Uint8Array containing e.g. [0xf8, 0x00]:

var a = new Uint8Array([0xf8, 0x00])

you can treat this as array of Int16 values instead:

var view = new DataView(a.buffer)
var val = view.getInt16(0, false);   // false for big-endian
> -2048

If the data in your array is the other way around (little-endian) supply true for the second parameter to .getInt16().

Alnitak
  • 334,560
  • 70
  • 407
  • 495