5

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.com/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.

DeathNote
  • 254
  • 1
  • 3
  • 13
  • *"is different from what I am expecting"* - What are you expecting and why? – Artjom B. Apr 05 '17 at 19:07
  • I am expecting decimal value `-139853185`? Because that's what people generally use for hex to decimal in js. @ArtjomB. – DeathNote Apr 05 '17 at 19:11
  • I have tried endian swapping as well. That didn't work either :l – DeathNote Apr 05 '17 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 Apr 05 '17 at 19:25

4 Answers4

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);
Community
  • 1
  • 1
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
4

Node can do that pretty easily using buffers:

Buffer.from('7F02AAF7', 'hex').readInt32LE()
robertklep
  • 198,204
  • 35
  • 394
  • 381
  • 1
    Thank you Robert for this solution. – DeathNote Apr 05 '17 at 20:19
  • What if you don't know the length of the hex string? – Robin-Hoodie Jan 07 '22 at 14:24
  • @Robin-Hoodie for an INT32, which is what the question was about, the length is fixed. What are you trying to do? – robertklep Jan 08 '22 at 12:26
  • I have a variable which stores a hex string for which I don't know the length up front. I could check the length manually and then call `readInt32LE|readInt16LE|...` but it seems like there should be an easier way. Your answer does answer the question though, so feel free to ignore – Robin-Hoodie Jan 10 '22 at 10:24
  • @Robin-Hoodie I think checking the length and picking the right method is the only way – robertklep Jan 11 '22 at 09:23
3

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-complement 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.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
0

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.com 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.

Mr. Panda
  • 485
  • 3
  • 14