Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QtScript float from byte values
Forum Updated to NodeBB v4.3 + New Features

QtScript float from byte values

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 1.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    David King
    wrote on last edited by VRonin
    #1

    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

    1 Reply Last reply
    0
    • L Offline
      L Offline
      Leonardo
      wrote on last edited by
      #2

      Why can't you just use a regular array?

      var x = new Float32Array([1, 2, 3, 4])[0];
      console.log(x)
      
      1 Reply Last reply
      1
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        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

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        0
        • D Offline
          D Offline
          David King
          wrote on last edited by
          #4

          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: Float32Array

          I 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:

          0_1515662103933_1.png

          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

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            hi
            OP = original Poster :)
            Or "him who started the thread"

            1 Reply Last reply
            2

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved