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. qint32 values into QByteArray
Qt 6.11 is out! See what's new in the release blog

qint32 values into QByteArray

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 398 Views
  • 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
    Drazz
    wrote on last edited by Drazz
    #1

    Hello,

    Im running this code with Desktop Qt 5.15.2 GCC 64 bit.

    #include <QCoreApplication>
    #include <QByteArray>
    #include <QDebug>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        quint32 value = 0x11223344;
    
        QByteArray payload;
        payload.resize(4);
    
        qInfo() << "Payload size: " << payload.size();
    
        payload[0] = static_cast<quint8>((value & 0xFF000000) >> 24);
        payload[1] = static_cast<quint8>((value & 0x00FF0000) >> 16);
        payload[2] = static_cast<quint8>((value & 0x0000FF00) >> 8);
        payload[3] = static_cast<quint8>((value & 0x000000FF) );
        qInfo() << "Output: " << payload;
    
        qInfo() << "Expected hex: " << hex \
                << static_cast<quint8>((value & 0xFF000000) >> 24) \
                << static_cast<quint8>((value & 0x00FF0000) >> 16) \
                << static_cast<quint8>((value & 0x0000FF00) >> 8) \
                << static_cast<quint8>((value & 0x000000FF) );
    
        return a.exec();
    }
    
    

    Output:

    Payload size:  4
    Output:  "\x11\"3D"
    Expected hex:  11 22 33 44
    

    Why output and expected are not equal? How can I do to make output and expected equal?

    J.HilkJ 1 Reply Last reply
    0
    • D Drazz

      Hello,

      Im running this code with Desktop Qt 5.15.2 GCC 64 bit.

      #include <QCoreApplication>
      #include <QByteArray>
      #include <QDebug>
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
      
          quint32 value = 0x11223344;
      
          QByteArray payload;
          payload.resize(4);
      
          qInfo() << "Payload size: " << payload.size();
      
          payload[0] = static_cast<quint8>((value & 0xFF000000) >> 24);
          payload[1] = static_cast<quint8>((value & 0x00FF0000) >> 16);
          payload[2] = static_cast<quint8>((value & 0x0000FF00) >> 8);
          payload[3] = static_cast<quint8>((value & 0x000000FF) );
          qInfo() << "Output: " << payload;
      
          qInfo() << "Expected hex: " << hex \
                  << static_cast<quint8>((value & 0xFF000000) >> 24) \
                  << static_cast<quint8>((value & 0x00FF0000) >> 16) \
                  << static_cast<quint8>((value & 0x0000FF00) >> 8) \
                  << static_cast<quint8>((value & 0x000000FF) );
      
          return a.exec();
      }
      
      

      Output:

      Payload size:  4
      Output:  "\x11\"3D"
      Expected hex:  11 22 33 44
      

      Why output and expected are not equal? How can I do to make output and expected equal?

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @Drazz said in qint32 values into QByteArray:

      Why output and expected are not equal? How can I do to make output and expected equal?

      representation issues of qDebug() and QByteArray.

      use qInfo() << "Output: " << payload.toHex(' ');

      btw you should use QDataStream


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      D 1 Reply Last reply
      3
      • J.HilkJ J.Hilk

        @Drazz said in qint32 values into QByteArray:

        Why output and expected are not equal? How can I do to make output and expected equal?

        representation issues of qDebug() and QByteArray.

        use qInfo() << "Output: " << payload.toHex(' ');

        btw you should use QDataStream

        D Offline
        D Offline
        Drazz
        wrote on last edited by
        #3

        @J-Hilk <3

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

          Further to @J-Hilk's response, the four bytes in the array map to the string you see:

          Output:        \x11 \" 3  D   
          Expected hex:  11   22 33 44
          

          The backslash before the " is there because the output is designed to be a usable C++ string and you escape embedded quotes.

          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