Aquarius_Girl
Aquarius_Girl

Reputation: 22906

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?

Upvotes: 1

Views: 2012

Answers (4)

Tommylee2k
Tommylee2k

Reputation: 2731

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.

Upvotes: 0

Aquarius_Girl
Aquarius_Girl

Reputation: 22906

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

Upvotes: 0

Violet Giraffe
Violet Giraffe

Reputation: 33579

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

Upvotes: 3

David Schwartz
David Schwartz

Reputation: 182763

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?

Upvotes: 3

Related Questions