Important: Please read the Qt Code of Conduct -

QStringList to QByteArray??

  • Is there aby way how to pass QStringList to QByteArray? And then back, from QByteArray to QStringList?

  • What about serialization like?:

    QByteArray ba;
    QDataStream ds(&ba);
    ds << my_stringlist;

    QByteArray ba;
    QDataStream ds(&ba);
    ds >> my_stringlist;

  • May be.

    // edit: or:
    QByteArray tmp;
    for(int i = 0; i < args.count(); i++) {

  • Moderators

    Did you read the documentation? There are quite some ways to get a QByteArray from a QString and a QString from a QByteArray listed in the documentation of QString.

    Which one you want to use depends on which encoding you want/have in your QByteArray.

  • Tobias, he is asking not about conversion, but more about (de)serialization (QStringList <> QByteArray, not QString <> QByteArray) :) . Previous answers are quite full I think

  • Im facing problems by creating a QByteArray: I want to go from this QStringList: ("0a", "7e", "ff", "f7", "ba", "97", "63", "ba", "80", "02", "40", "00", "00", "00", "00", "2d", "00", "00", "00", "00")
    to a QByteArray. I have gone through the documentation but I dont get the expected result for example for the above string I get :

    I tried the examples given on this thread to obtain the above result.
    Do you know what Im doing wrong? Thanks

  • @Garza said in QStringList to QByteArray??:

    Do you know what Im doing wrong? Thanks

    It looks like you QStringList constains hexadeximal value as string.
    Why do you have create a QStringList and not a QString?

    Hexadecimal strings can be converted to QByteArray as follow: QByteArray b = QByteArray::fromHex(hexString.toLatin1());

    In your case, you have to convert first your QStringList to QString:

    QString hexString = stringList.join("");
    QByteArray b = QByteArray::fromHex(hexString.toLatin1());

  • Moderators

    @Garza would be nice if you showed us, what exactly you tried so far

    here is one, probably inefficient way:

    QStringList sl {"0a", "7e", "ff", "f7", "ba", "97", "63", "ba", "80", "02", "40", "00", "00", "00", "00", "2d", "00", "00", "00", "00"};
        QByteArray ba;
        for(const QString &s : qAsConst(sl)){
        qDebug() << ba << endl << ba.toHex(' ');

  • @KroMignon Thanks, I tried this solution and when using qDebug() I get:
    ("0a", "7e", "ff", "f7", "ba", "97", "63", "ba", "80", "02", "40", "00", "00", "00", "00", "2d", "00", "00", "00", "00")
    result of qDebug()<<b:
    is the representation of qDebug incorrect or why I dont see the same bytes as in the list?
    Im parsing a CAN data recording and the payload is in hex values. In order the use the QCANBus library I want to define a Qbytearray as payload for a CAN frame I define for each line of CAN recording.

  • @Garza said in QStringList to QByteArray??:

    result of qDebug()<<b:

    You shoud try: qDebug() << QString(b.toHex());

  • This post is deleted!

  • @KroMignon Thanks

Log in to reply