Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hex String to INT32 - Little Endian (DCBA Format) Javascript

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.

like image 248
DeathNote Avatar asked Jan 04 '23 05:01

DeathNote


1 Answers

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);
like image 159
Nina Scholz Avatar answered Jan 16 '23 22:01

Nina Scholz