I would like to parse a two-byte value that is "expressed in a signed 8.8 fixed-point notation". Let's assume that I have the two bytes in the hexadecimal format below.
let data = '1800';
The 0x1800
in hexadecimal 8.8 fixed point notation should be 24 when converted.
Another example: 0x8000
in hexadecimal signed 8.8 fixed point notation should be -128 when converted.
I'm specifically attempting to parse the temperature from an Eddystone Telemetry frame, which is defined here: .md#field-notes
I would like to parse a two-byte value that is "expressed in a signed 8.8 fixed-point notation". Let's assume that I have the two bytes in the hexadecimal format below.
let data = '1800';
The 0x1800
in hexadecimal 8.8 fixed point notation should be 24 when converted.
Another example: 0x8000
in hexadecimal signed 8.8 fixed point notation should be -128 when converted.
I'm specifically attempting to parse the temperature from an Eddystone Telemetry frame, which is defined here: https://github./google/eddystone/blob/master/eddystone-tlm/tlm-plain.md#field-notes
parseInt()
function?
– Pointy
Commented
Aug 20, 2019 at 0:17
0x18
is 24, not 27.
– Pointy
Commented
Aug 20, 2019 at 0:19
parseInt(data, 16) / 256
– Khauri
Commented
Aug 20, 2019 at 0:34
You can create a prototype from a custom object. Like this:
function FixedPoint(fraction){
this.fraction = fraction;
}
FixedPoint.prototype.calculate = function(value){
let intValue = parseInt(value, 16);
let signed = (intValue & 0x8000) > 0 ? -1 : 1;
return signed * intValue / Math.pow(2, this.fraction);
}
How to use it?
let example = new FixedPoint(8);
example.calculate('1840');
returns 24.25
More info about fixed point here
You can shift the value left so that its sign bit lines up with JavaScript’s 32-bit signed integers:
let data = 0x8000; // = parseInt('8000', 16);
data << 16 // -2147483648
Then divide that so the high byte represents 0–255:
(data << 16) / (1 << 24) // -128