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. Convert function to qt notation

Convert function to qt notation

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 1.2k Views 1 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
    Damian7546
    wrote on last edited by Damian7546
    #1

    Hi,
    Please help how convert this line: buffer.writeBigInt64LE(BigInt(number)) to qt ?
    JS:

    function int64LE(number) {
      const buffer = Buffer.alloc(8)
      buffer.writeBigInt64LE(BigInt(number))
      return buffer
    }
    

    qt:

    QByteArray int64LE(quint64 number)
    {
        QByteArray buffer[8];
        ......
        return buffer;
    
    }
    
    1 Reply Last reply
    0
    • D Damian7546

      @Christian-Ehrlicher
      Still doesn't work:

      QByteArray qint64_2_LE(qint64 val)
      {
          QByteArray le;
          le.resize(8);
          qToLittleEndian(val, &le);
          return le;
      }
      

      Application crashed after call qToLittleEndian function.

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #9

      @Damian7546 said in Convert function to qt notation:

      qToLittleEndian(val, &le);

      qToLittleEndian writes the result into the memory block the second parameter points to. And you're passing pointer to QByteArray!
      It should be:

      qToLittleEndian(val, le.data());
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • GaoboG Offline
        GaoboG Offline
        Gaobo
        wrote on last edited by
        #2
        This post is deleted!
        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by Christian Ehrlicher
          #3

          Why manually when there are helper functions?
          https://doc.qt.io/qt-6/qtendian.html

          Oh, now deleted for unknown reasons.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          D 1 Reply Last reply
          0
          • GaoboG Offline
            GaoboG Offline
            Gaobo
            wrote on last edited by
            #4

            Maybe you can ask chatgpt.....
            This answer is from deepseek:

            To convert the JavaScript function int64LE to Qt, you need to create a QByteArray that holds the little-endian byte representation of a 64-bit signed integer. Here's the step-by-step solution:

            1. Resize the QByteArray to hold 8 bytes (64 bits).
            2. Cast the input quint64 number to qint64 to handle it as a signed integer.
            3. Extract each byte in little-endian order by masking and shifting the value.
            4. Store each byte in the QByteArray.
            QByteArray UtilsEssp::int64LE(quint64 number)
            {
                QByteArray buffer;
                buffer.resize(8); // Allocate 8 bytes
                qint64 value = static_cast<qint64>(number); // Treat as signed
            
                // Extract each byte in little-endian order
                for (int i = 0; i < 8; ++i) {
                    buffer[i] = static_cast<char>(value & 0xFF);
                    value >>= 8;
                }
            
                return buffer;
            }
            

            Explanation:

            • Resizing the Buffer: buffer.resize(8) ensures the QByteArray has space for 8 bytes.
            • Casting to Signed: static_cast<qint64>(number) converts the unsigned input to a signed 64-bit integer, preserving the bit pattern.
            • Little-Endian Extraction: The loop iterates 8 times, each time taking the least significant byte using value & 0xFF, then right-shifting value by 8 bits. This fills the QByteArray from the least significant byte to the most significant, achieving little-endian order.

            This approach mirrors the original JavaScript functionality by directly manipulating the byte representation, ensuring compatibility with systems expecting a signed 64-bit integer in little-endian format.

            1 Reply Last reply
            0
            • D Damian7546 has marked this topic as solved on
            • Christian EhrlicherC Christian Ehrlicher

              Why manually when there are helper functions?
              https://doc.qt.io/qt-6/qtendian.html

              Oh, now deleted for unknown reasons.

              D Offline
              D Offline
              Damian7546
              wrote on last edited by Damian7546
              #5

              @Christian-Ehrlicher said in Convert function to qt notation:

              Why manually when there are helper functions?
              https://doc.qt.io/qt-6/qtendian.html

              Oh, now deleted for unknown reasons.

              One way I use:

              QByteArray m_data = QByteArray("\x0c\x06\x04\x04\x02\x00\x02\x00",8);
              qint64 value =  qFromLittleEndian<qint64>(m_data);
              

              but how change value to m_data using QtEndian and function qToLittleEndian ?

              will the following be ok?

               QByteArray m_data;
                qToLittleEndian(value, &m_data );
              
              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #6

                You should resize the QByteArray before

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                D 2 Replies Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  You should resize the QByteArray before

                  D Offline
                  D Offline
                  Damian7546
                  wrote on last edited by
                  #7
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • D Damian7546 has marked this topic as solved on
                  • Christian EhrlicherC Christian Ehrlicher

                    You should resize the QByteArray before

                    D Offline
                    D Offline
                    Damian7546
                    wrote on last edited by
                    #8

                    @Christian-Ehrlicher
                    Still doesn't work:

                    QByteArray qint64_2_LE(qint64 val)
                    {
                        QByteArray le;
                        le.resize(8);
                        qToLittleEndian(val, &le);
                        return le;
                    }
                    

                    Application crashed after call qToLittleEndian function.

                    jsulmJ 1 Reply Last reply
                    0
                    • D Damian7546

                      @Christian-Ehrlicher
                      Still doesn't work:

                      QByteArray qint64_2_LE(qint64 val)
                      {
                          QByteArray le;
                          le.resize(8);
                          qToLittleEndian(val, &le);
                          return le;
                      }
                      

                      Application crashed after call qToLittleEndian function.

                      jsulmJ Online
                      jsulmJ Online
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #9

                      @Damian7546 said in Convert function to qt notation:

                      qToLittleEndian(val, &le);

                      qToLittleEndian writes the result into the memory block the second parameter points to. And you're passing pointer to QByteArray!
                      It should be:

                      qToLittleEndian(val, le.data());
                      

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      3
                      • D Damian7546 has marked this topic as solved on

                      • Login

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