Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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++) {
    tmp.append(args.at(i).toAscii());
    tmp.append(",");
    }
    @


  • 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 :
    "\x00,\x00,\x00,\x00,\x00,a,?,\x00,P,\x02,(,\x00,\x00,\x00,\x00,\x00,\x00,\x00,\x00,\x00,"

    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;
        ba.reserve(sl.size());
        for(const QString &s : qAsConst(sl)){
            ba.append(s.toUShort(nullptr,16));
        }
    
        qDebug() << ba << endl << ba.toHex(' ');
    


  • @KroMignon Thanks, I tried this solution and when using qDebug() I get:
    list:
    ("0a", "7e", "ff", "f7", "ba", "97", "63", "ba", "80", "02", "40", "00", "00", "00", "00", "2d", "00", "00", "00", "00")
    result of qDebug()<<b:
    "\n~\xFF\xF7\xBA\x97""c\xBA\x80\x02@\x00\x00\x00\x00-\x00\x00\x00\x00"
    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