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. [Solved]Convert from qint32 to qbytearray
Forum Updated to NodeBB v4.3 + New Features

[Solved]Convert from qint32 to qbytearray

Scheduled Pinned Locked Moved General and Desktop
9 Posts 7 Posters 15.5k 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.
  • A Offline
    A Offline
    antonyprojr
    wrote on last edited by
    #1

    How to convert from qint32 to 4bytes QByteArray and how to convert back from QByteArray to qint32?

    1 Reply Last reply
    0
    • B Offline
      B Offline
      butterface
      wrote on last edited by
      #2

      Is that what you are looking for: "setNum":http://qt-project.org/doc/qt-4.8/qbytearray.html#setNum
      If not, maybe use a QDataStream and << num
      and if also not I did not understand your question.

      1 Reply Last reply
      0
      • N Offline
        N Offline
        N3Roaster
        wrote on last edited by
        #3

        If the QByteArray is only going to contain a qint32, you could use fromRawData. Alternately you can create a char* with the address of the integer and append from there. The latter technique works in reverse as well to get the qint32 back from the byte array.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MuldeR
          wrote on last edited by
          #4

          [quote author="butterface" date="1349040456"]Is that what you are looking for: "setNum":http://qt-project.org/doc/qt-4.8/qbytearray.html#setNum
          If not, maybe use a QDataStream and << num
          and if also not I did not understand your question.[/quote]

          That would first convert the number to a "printable" ASCII string and then set the QByteArray these characters. If you want to create a QByteArray containing the "raw" bits of a qint32 value, you can do something like like:
          @qint32 x = 42;
          QByteArray foo = QByteArray::fromRawData(reinterpret_cast<char*>(&x), sizeof(qint32));@ The above won't copy the data though, it just wraps the pointer. To copy the data, you may use: @qint32 x = 42;
          QByteArray foo(sizeof(qint32), '\0'); //Create QByteArray of required size
          memcpy(foo.data(), &x, sizeof(qint32)); //Copy the data into the QByteArray@ Should also work: @qint32 x = 42;
          QByteArray foo(reinterpret_cast<char*>(&x), sizeof(qint32));@

          My OpenSource software at: http://muldersoft.com/

          Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

          Go visit the coop: http://youtu.be/Jay...

          1 Reply Last reply
          0
          • A Offline
            A Offline
            antonyprojr
            wrote on last edited by
            #5

            This code works:

            @QByteArray array;
            qint32 x = 11;
            array.append((const char*)&x, sizeof(x));@

            How to convert it back to qint32?

            1 Reply Last reply
            0
            • C Offline
              C Offline
              ChrisW67
              wrote on last edited by
              #6

              You can use MuldeR's memcpy in reverse
              @
              // in
              qint32 x = 42;
              QByteArray foo(sizeof(qint32), '\0'); //Create QByteArray of required size
              memcpy(foo.data(), &x, sizeof(qint32)); //Copy the data into the QByteArray
              //out
              qint32 y;
              Q_ASSERT(foo.size() > sizeof(y));
              memcpy(&y, foo.data(), sizeof(y));
              @

              or even
              @
              //out
              Q_ASSERT(foo.size() > sizeof(qint32));
              qint32 y = (reinterpret_cast<qint32>(foo.data()));
              @

              If in and out occur on different machines then you must consider the order that bytes are written in.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                I'd just use a QDataStream:

                @
                //write
                QByteArray target;
                QDataStream s(&target);
                qint32 value = someValue;
                s << value;

                //read
                qint32 value;
                QByteArray source;
                QDataStream s(&source);
                s >> value;
                @

                This way is much saver than using direct casts. It will work across small and big endian systems, for instance.

                M 1 Reply Last reply
                2
                • A Offline
                  A Offline
                  antonyprojr
                  wrote on last edited by
                  #8

                  Thanks for any answer, this is the solution for me:
                  @
                  //write
                  QByteArray target;
                  QDataStream s(&target, QIODevice::ReadWrite);
                  qint32 value = someValue;
                  s << value;

                  //read
                  qint32 value;
                  QByteArray source;
                  QDataStream s(&source, QIODevice::ReadWrite);
                  s >> value;
                  @

                  1 Reply Last reply
                  1
                  • A andre

                    I'd just use a QDataStream:

                    @
                    //write
                    QByteArray target;
                    QDataStream s(&target);
                    qint32 value = someValue;
                    s << value;

                    //read
                    qint32 value;
                    QByteArray source;
                    QDataStream s(&source);
                    s >> value;
                    @

                    This way is much saver than using direct casts. It will work across small and big endian systems, for instance.

                    M Offline
                    M Offline
                    Mohammad Kanan
                    wrote on last edited by
                    #9

                    @andre ++1 for mentioning the "Endian" systems

                    1 Reply Last reply
                    0

                    • Login

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