最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Hex String to INT32 - Little Endian (DCBA Format) Javascript - Stack Overflow

programmeradmin3浏览0评论

Implementing something based on a pathetic documentation without no info nothing.

The example is just this

(7F02AAF7)H => (F7AA027F)H = -139853185

Let's say even if I convert 7F02AAF7 to F7AA027F, then still the output via 'parseInt('F7AA027F', 16)' is different from what I am expecting.

I did some google search and found this website /

Here when you input 7F02AAF7 then it is processed to wanted number under INT32 - Little Endian (DCBA) system. I tried this search term but no luck. Can you please tell me what exactly am I supposed to do here and is there any node.js library which can help me with this.

Implementing something based on a pathetic documentation without no info nothing.

The example is just this

(7F02AAF7)H => (F7AA027F)H = -139853185

Let's say even if I convert 7F02AAF7 to F7AA027F, then still the output via 'parseInt('F7AA027F', 16)' is different from what I am expecting.

I did some google search and found this website http://www.scadacore./field-tools/programming-calculators/online-hex-converter/

Here when you input 7F02AAF7 then it is processed to wanted number under INT32 - Little Endian (DCBA) system. I tried this search term but no luck. Can you please tell me what exactly am I supposed to do here and is there any node.js library which can help me with this.

Share Improve this question asked Apr 5, 2017 at 19:02 DeathNoteDeathNote 2641 gold badge3 silver badges14 bronze badges 4
  • "is different from what I am expecting" - What are you expecting and why? – Artjom B. Commented Apr 5, 2017 at 19:07
  • I am expecting decimal value -139853185? Because that's what people generally use for hex to decimal in js. @ArtjomB. – DeathNote Commented Apr 5, 2017 at 19:11
  • I have tried endian swapping as well. That didn't work either :l – DeathNote Commented Apr 5, 2017 at 19:20
  • The parseInt() function is not going to recognize that leading F as meaning that the number is negative. Without a leading "-" it will produce a positive value. – Pointy Commented Apr 5, 2017 at 19:25
Add a ment  | 

4 Answers 4

Reset to default 6

You could adapt the excellent answer of T.J. Crowder and use DataView#setUint8 for the given bytes with DataView#getInt32 and an indicator for littleEndian.

var data = '7F02AAF7'.match(/../g);

// Create a buffer
var buf = new ArrayBuffer(4);
// Create a data view of it
var view = new DataView(buf);

// set bytes
data.forEach(function (b, i) {
    view.setUint8(i, parseInt(b, 16));
});

// get an int32 with little endian
var num = view.getInt32(0, 1);
console.log(num);

Node can do that pretty easily using buffers:

Buffer.from('7F02AAF7', 'hex').readInt32LE()

JavaScript integers are usually stored as a Number value:

4.3.19
primitive value corresponding to a double-precision 64-bit binary format IEEE 754 value

So the result of parseInt is a float where the value losslessly fits into the fraction part of the float (52 bits capacity). parseInt also doesn't parse it as two-plement notation.

If you want to force anything that you read into 32 bit, then the easiest would be two force it to be automatically converted to 32 bit by applying a binary operation. I would suggest:

parseInt("F7AA027F", 16) | 0

The binary OR (|) with 0 is essentially a no-op, but it converts any integer to 32 bit. This trick is often used in order to convert numbers to 32 bit in order to make calculations on it faster.

Also, this is portable.

In my case, I am trying to send accelerometer data from Arduino to Pi.

The raw data I am reading is that [0x0, 0x0, 0x10, 0xBA].

If you lack knowledge about the topic as me, use the scadacore. website to find out find your data should correspond to. In my case it is Float - Little Endian (DCBA) which outputs: -0.03126526. Now we know what kind of a conversion that we need.

Then, check available functions based on the language. In my case, Node.js buffer library offers buf.readFloatLE([offset]) function which is the one I need.

发布评论

评论列表(0)

  1. 暂无评论