QByteArray and QDataStream



  • 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();
    


  • @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;
    

    ?



  • @Sunfluxgames said in qbytearray and datastream:

    socket->write(info);

    You can use datastream directly on the QIODevice:

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


  • 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.



  • @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)


  • Qt Champions 2016

    @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;
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.