Search code examples
c++qtqstringqchar

Pushing a QChar in QString


#include <QCoreApplication>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a (argc, argv);

    unsigned char VEL_LEFT = 0;
    VEL_LEFT  = VEL_LEFT | 100;

    QString packet;
    packet.push_back (QChar (VEL_LEFT));

    qDebug () << "VEL_LEFT: " << VEL_LEFT;
    qDebug () << "packet: " << packet;

    return a.exec();
}

The intention here is to manipulate 8 bits by ORing or ANDing them, and then storing them in a string.

Then I would like to read the output as decimal.

Output of this program is:

VAL_LEFT: 100
packet: "d"

What is "d" here?


Solution

  • I want to first manipulate 8 bits by ORing or ANDing them. Then I want to store them in a QString, and then I would like to read the output as "decimal".

    Following works.

    #include <QCoreApplication>
    #include <QDebug>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a (argc, argv);
    
        unsigned char SOP         = 0;
        unsigned char E_STOP      = 0;
        unsigned char DIRECTION   = 0;
        unsigned char VEL_LEFT    = 0;
    
        QString packet;
    
        packet.append (SOP);
    
        E_STOP    = E_STOP | 1;
        packet.append (E_STOP);
    
        DIRECTION = DIRECTION | 3;
        packet.append (DIRECTION);
    
        VEL_LEFT  = VEL_LEFT | 100;
        packet.push_back (VEL_LEFT);
    
        qDebug () << "VEL_LEFT: " << VEL_LEFT;
    
        qDebug () << "packet0: " << (int)packet[0].toLatin1();
        qDebug () << "packet1: " << (int)packet[1].toLatin1();
        qDebug () << "packet2: " << (int)packet[2].toLatin1();
        qDebug () << "packet3: " << (int)packet[3].toLatin1();
    
        qDebug () << "packet: " << packet.length();
    
        return a.exec();
    }