简体   繁体   中英

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?

100 is the way your machine encodes a lower case d character. So

char x = 'd';

is the same as

char x = 100;

You may be confusing numbers with representations. One hundred is a number. It can be represented as the string "100" or by a hundred cars or by a sentence like "one more than ninety-nine'. It is the same number however represented.

"100" is a representation. In base 10 it represents the number one hundred. But in other representation systems, it can represent other numbers.

You made the string store the number one hundred. Then you printed it using your system's character representation. Well, your system uses the number on hundred to represent the character "d".

What do you want your string to store? The number one hundred? The characters "1", "0", "0"? Or what?

Looks like you want
packet.push_back (QString::number(VEL_LEFT));

instead of
packet.push_back (QChar (VEL_LEFT));

QString::number takes a number and returns a string with that number's representation.
E. g. if VEL_LEFT equals 100, QString::number will return the string "100".

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

what you probably wanted is this?

unsigned char tmpString[4];
*(uint32_t *)tmpString = 3158065;

"100" is (like if you type it on your keyboard) a "1" (ascii 49) followed by two "0" (ascii 48) and a 0 to determine the end of the string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM