1
#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?

Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411

4 Answers4

3

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?

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • You did store the number 100 in the string. But then you printed out the contents of the string as a character. The number 100 is the same as the character `d` in the representation your system uses. (Just as the number ten is the same as the digit string "10" in base 10 representation.) If the string stores a number, you have to print its output as a number. – David Schwartz Jan 21 '16 at 07:34
  • (Or do you mean you want the string to store a representation of the number 100 in base 10?) – David Schwartz Jan 21 '16 at 07:35
  • Violet's answer does not make the string store the number 100. It makes it store the string "100". (Which can represent one hundred, or four in binary, or any number of other things.) – David Schwartz Jan 21 '16 at 07:38
  • @VioletGiraffe That would leave this particular OP with a very serious misunderstanding about the distinction between values and representations that would be devastating to his future programming efforts. – David Schwartz Jan 21 '16 at 07:40
  • @DavidSchwartz Thanks for further explanation and understanding my confusion. I will explain again. I want to first manipulate 8 bits by oring or anding them. Then I want to store them in a string. Then I would like to read the output as decimal. – Aquarius_Girl Jan 21 '16 at 07:58
3

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

Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
0

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();
}
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
0

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.

Tommylee2k
  • 2,683
  • 1
  • 9
  • 22