QtScript float from byte values
-
Hi,
I’m looking to place 4 values, into a 4-byte array, as follows:
var data = new Uint8Array(4); data[0] = d0; data[1] = d1; data[2] = d2; data[3] = d3;
However, the Qt Script debugger, errored as below:
Uncaught exception at foo.js:96: ReferenceError: Can't find variable: Uint8Array
I read that Uint8Array only debuted in 2017, so perhaps unsupported.
If so, is there a more basic way to create a 4-byte array, or 4-byte buffer,
which I can access bytewise ?Instead of Uint8Array, I tried also ArrayBuffer, and char, but likewise errored.
I'm a newcomer to Qt and JavaScript, I'm sure I'm doing something comically wrong..
Ultimately, I'd like to do 'new Float32Array(new Uint8Array([d0, d1, d2, d3]).buffer)[0]',
to view my 4 bytes as an IEEE-74 32-bit float.Best regards,
David
-
I don't think OP needs a Float32Array but rather to build a single 32bit float from 4 bytes that are d0, d1, d2 and d3.
In C++ it would be:const char binaryData[4]={d0, d1, d2 ,d3}; float result; std::memcpy(&result,binaryData,4);
I have no idea how to translate it to QtScript js
-
Hi Leonardo,
First of, thanks very much for your post - I'm really impressed with how folks offer help on this forum, quite quickly
Hmm, first of I tried the code you posted there. But I get error as below:
Uncaught exception at C:/1/TouchPDT.js:49: ReferenceError: Can't find variable: Float32ArrayI also tried removing the new, as I have some inkling it's now allowed in a .js, but I might be entirely wrong there !
But the editor complained thus:Is this code syntax to initialise a 32-bit IEEE754 float, with the 4 bytes values that comprise the underlying representation of the float ?
You queried why I could not just use a regular array. From what I’ve read about JavaScript generic arrays,
they are a far more abstract concept, with an underlying memory storage model that includes for each element,
a forward pointer and back pointer, to allow array member iteration backwards and forwards, also a pointer to the actual data.
I might be completely wrong though :-)Best regards,
David
Hi VRonin,
Likewise thank you.
Err, what does OP stand for ?
About your code, yes I'd do it that way in C / C++ too :-)
Or even, to avoid the memcpy need, as follows:union { char binaryData[4]; float result; } foo; foo.binaryData[0] = d0; foo.binaryData[1] = d1; foo.binaryData[2] = d2; foo.binaryData[3] = d3; console.log(foo.result); // Show
Best regards,
David
-
hi
OP = original Poster :)
Or "him who started the thread"