2

I need to convert an array of 8 bytes into a double. This is my starting array:

[170, 85, 255, 63, 205, 171, 170, 85]

and it should convert to -9.591053231630682E-105.

Each element of the array can be from 0 to 256.

Chuender
  • 35
  • 6

1 Answers1

1

You could adapt the excellent answer of T.J. Crowder and use DataView#setUint8 for the given bytes.

var data =  [170, 85, 255, 63, 205, 171, 170, 85];

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

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

// Read the bits as a float
var num = view.getFloat64(0);
// Done
console.log(num);
Nina Scholz
  • 323,592
  • 20
  • 270
  • 324