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.

1 Answer 1

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);

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.