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. QByteArray and QDataStream
Forum Updated to NodeBB v4.3 + New Features

QByteArray and QDataStream

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 5 Posters 12.3k 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.
  • S Offline
    S Offline
    Sunfluxgames
    wrote on last edited by A Former User
    #1

    So ive got a QbyteArray that i've decided to create with the ability to insert data into the array.

    Looking through the docs ive noticed that insert only takes a char as a argument. Can i cast the char and it still be safe.
    Example:

    #define books 204
    Qstring title = "Read Me Please"
    Qbytearray info;
    info.fill(0,50);
    info.insert(0,books);
    info.insert(1,title);
    info.resize(0,50);
    socket->write(info);
    

    can i do?

    #define books 204
    static_cast<unsigned char>(books)
    info.insert(0,books);
    

    Also looking to create a log file from this qbytearray am i better off using datastream to write the hex to file?

    QByteArray data;
    QDataStream ds(&data, QIODevice::ReadWrite);
    
    ds << data.tohex() << endl;
    ds.flush();
    ds.close();
    
    Taz742T VRoninV 2 Replies Last reply
    0
    • S Sunfluxgames

      So ive got a QbyteArray that i've decided to create with the ability to insert data into the array.

      Looking through the docs ive noticed that insert only takes a char as a argument. Can i cast the char and it still be safe.
      Example:

      #define books 204
      Qstring title = "Read Me Please"
      Qbytearray info;
      info.fill(0,50);
      info.insert(0,books);
      info.insert(1,title);
      info.resize(0,50);
      socket->write(info);
      

      can i do?

      #define books 204
      static_cast<unsigned char>(books)
      info.insert(0,books);
      

      Also looking to create a log file from this qbytearray am i better off using datastream to write the hex to file?

      QByteArray data;
      QDataStream ds(&data, QIODevice::ReadWrite);
      
      ds << data.tohex() << endl;
      ds.flush();
      ds.close();
      
      Taz742T Offline
      Taz742T Offline
      Taz742
      wrote on last edited by Taz742
      #2

      @Sunfluxgames said in qbytearray and datastream:

      QByteArray data;
      QDataStream ds(&data, QIODevice::ReadWrite);

      ds << data.tohex() << endl;
      ds.flush();
      ds.close();

      Which is written in ds it will be converted to ByteArray. to speak Roughly

      what does it mean

      ds << data.tohex() << endl;
      

      ?

      Do what you want.

      1 Reply Last reply
      0
      • S Sunfluxgames

        So ive got a QbyteArray that i've decided to create with the ability to insert data into the array.

        Looking through the docs ive noticed that insert only takes a char as a argument. Can i cast the char and it still be safe.
        Example:

        #define books 204
        Qstring title = "Read Me Please"
        Qbytearray info;
        info.fill(0,50);
        info.insert(0,books);
        info.insert(1,title);
        info.resize(0,50);
        socket->write(info);
        

        can i do?

        #define books 204
        static_cast<unsigned char>(books)
        info.insert(0,books);
        

        Also looking to create a log file from this qbytearray am i better off using datastream to write the hex to file?

        QByteArray data;
        QDataStream ds(&data, QIODevice::ReadWrite);
        
        ds << data.tohex() << endl;
        ds.flush();
        ds.close();
        
        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        @Sunfluxgames said in qbytearray and datastream:

        socket->write(info);

        You can use datastream directly on the QIODevice:

        QDataStream ds(socket);
        ds << books << title;
        

        "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
        1
        • S Offline
          S Offline
          Sunfluxgames
          wrote on last edited by Sunfluxgames
          #4

          In this example we create an array of data then we send it to a function to be logged..

          I'd just like to log the data as hex nothing more.

          // Some other function
          #define books 204
          Qstring title = "Read Me Please"
          Qbytearray info;
          info.fill(0,50);
          info.insert(0,books);
          info.insert(1,title);
          info.resize(0,50);
          socket->write(info);
          
          WriteLog(socketinfo.txt, info);
          //
          
          void WriteLog(QString filename, QbyteArray buffer)
          {
              QFile file(filename);
              if(!file.open(QFile:WriteOnly |
                            QFile::Text))
              {
                  return;
              }
          
             QDataStream out(&file);
          
           // write array of buffer to hex  .... endl used to end line...
             out << buffer.tohex() << endl;
             // flush and close
          file.flush();
          file.close();
          }
          

          Also can i do this?

          #define books 204
          info.insert(0,static_cast<unsigned char>(books)); // since char can only hold 128 and unsigned char can hold 255
          

          Sorry for the junk code writting it off the top of my head.

          DiracsbracketD kshegunovK 2 Replies Last reply
          0
          • S Sunfluxgames

            In this example we create an array of data then we send it to a function to be logged..

            I'd just like to log the data as hex nothing more.

            // Some other function
            #define books 204
            Qstring title = "Read Me Please"
            Qbytearray info;
            info.fill(0,50);
            info.insert(0,books);
            info.insert(1,title);
            info.resize(0,50);
            socket->write(info);
            
            WriteLog(socketinfo.txt, info);
            //
            
            void WriteLog(QString filename, QbyteArray buffer)
            {
                QFile file(filename);
                if(!file.open(QFile:WriteOnly |
                              QFile::Text))
                {
                    return;
                }
            
               QDataStream out(&file);
            
             // write array of buffer to hex  .... endl used to end line...
               out << buffer.tohex() << endl;
               // flush and close
            file.flush();
            file.close();
            }
            

            Also can i do this?

            #define books 204
            info.insert(0,static_cast<unsigned char>(books)); // since char can only hold 128 and unsigned char can hold 255
            

            Sorry for the junk code writting it off the top of my head.

            DiracsbracketD Offline
            DiracsbracketD Offline
            Diracsbracket
            wrote on last edited by Diracsbracket
            #5

            @Sunfluxgames said in QByteArray and QDataStream:

            #define books 204

            Instead of using #define, why don't you use

            const unsigned char books = 204;
            

            or

            static const unsigned char books = 204;
            

            So you the compiler knows exactly what data type you mean by 204 (it could be a 32-bit integer)

            1 Reply Last reply
            1
            • S Sunfluxgames

              In this example we create an array of data then we send it to a function to be logged..

              I'd just like to log the data as hex nothing more.

              // Some other function
              #define books 204
              Qstring title = "Read Me Please"
              Qbytearray info;
              info.fill(0,50);
              info.insert(0,books);
              info.insert(1,title);
              info.resize(0,50);
              socket->write(info);
              
              WriteLog(socketinfo.txt, info);
              //
              
              void WriteLog(QString filename, QbyteArray buffer)
              {
                  QFile file(filename);
                  if(!file.open(QFile:WriteOnly |
                                QFile::Text))
                  {
                      return;
                  }
              
                 QDataStream out(&file);
              
               // write array of buffer to hex  .... endl used to end line...
                 out << buffer.tohex() << endl;
                 // flush and close
              file.flush();
              file.close();
              }
              

              Also can i do this?

              #define books 204
              info.insert(0,static_cast<unsigned char>(books)); // since char can only hold 128 and unsigned char can hold 255
              

              Sorry for the junk code writting it off the top of my head.

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by kshegunov
              #6

              @Sunfluxgames said in QByteArray and QDataStream:

              Qstring title = "Read Me Please"
              Qbytearray info;
              info.fill(0,50);
              info.insert(0,books);
              info.insert(1,title);
              info.resize(0,50);
              

              This is completely bogus. Use a QDataStream instance to write to the byte array (just like what @VRonin showed above).

              QByteArray info;
              QDataStream out(&info, QIODevice::WriteOnly);
              
              out << books << title;
              

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              4

              • Login

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