How to parse a hex value that is represented by signed 8.8 fixed-point notation? (JavaScript) - Stack Overflow

I would like to parse a two-byte value that is "expressed in a signed 8.8 fixed-point notation&quo

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.

More details

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.

More details

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

Share Improve this question edited Aug 20, 2019 at 3:00 claytonkucera asked Aug 20, 2019 at 0:16 claytonkuceraclaytonkucera 3771 gold badge3 silver badges13 bronze badges 5
  • 1 Have you checked out the documentation for the parseInt() function? – Pointy Commented Aug 20, 2019 at 0:17
  • 1 Also 0x18 is 24, not 27. – Pointy Commented Aug 20, 2019 at 0:19
  • 3 Did you mean to tag javascript? Vanilla JS doesn't have types identifiers like that. If you're using a different language it'd probably be the equivalent of: parseInt(data, 16) / 256 – Khauri Commented Aug 20, 2019 at 0:34
  • @Khauri I did. Thanks for pointing that out, updated. – claytonkucera Commented Aug 20, 2019 at 1:20
  • Hi @Khauri This looks mostly right with one exception. Since it's signed 8.8 fixed point notation, it needs to handle potentially negative numbers. Specifically the value can range between 128 and -128. – claytonkucera Commented Aug 20, 2019 at 2:18
Add a ment  | 

2 Answers 2

Reset to default 3

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745648299a4638103.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信