One letter string into char and then bit-wise comparing



  • Hello,

    first thanks to the community to have helped me with qtcpserver, qtcpclient and sockets ....

    now what i want do:
    from the robot i send strings via tcpip to my qt-gui-program, and in some of those strings i have one statusbyte at the end:
    qstring statusbyte = line.right(1)

    in this byte, 6 bits tells me about robots status, so what i want to do after i got this byte, is to check it bitwise with the if-operator, and then i want to make signals to the gui, so the woman (in or company a lot of woman work at the machines... ) can see in gui about robots status.

    how do i convert the one sign qstring in a byte and then how do i check its bits in qt-style?

    thanks a lot and nice weekend



  • Hi. You can send QByteArray insted of QString.
    @
    QByteArray data = QString("data that you want to transfer").toAscii();
    QByteArray statusbyte = line.right(1);
    QByteArray pack = data+statusbyte;

    // and now to convert

    QBitArray = pack.right(1);
    QString data = pack.left( pack.size()-1 ); // without statusbyte

    @



  • You can use at() or constData() to get array elements from a QString or QByteArray.

    e.g.
    QByteArray data("test stringX");
    char status = data.at(data.length() - 1);
    // status == 'X'

    You can use QFlags or just the C++ integer & operator to test bits in the status byte.



  • hi,

    thanks for the advices,
    now i have one char or qbytearray of dimension 1.

    how do i check now if bit 3 is 0 or 1?
    i would like to get a bool with testflag but it doesnt work fore me yet.
    i dont unterstand exactly how to do it with qflags,
    can s.o. give me an advice?
    Q_DECLARE_OPERATORS_FOR_FLAGS(?)

    thanks



  • Did you actually create a QFlags type with the relevant enum?
    Something like:

    @
    enum RobotStatusFlag {
    StatusOne = 0x1,
    StatusTwo = 0x2,
    StatusThree = 0x4,
    StatusFoo = 0x8,
    StatusBar = 0x10
    }
    Q_DECLARE_FLAGS(RobotStatusFlag, RobotStatusFlags);

    Q_DECLARE_OPERTORS_FOR_FLAGS(RobotStatusFlags);
    @

    That would allow you to do something like:
    @
    RobotStatusFlags status(data.at(data.length() -1);
    if (status.testFlag(StatusFoo)) {
    // do something foo-like
    }
    @



  • thanks, that helped a lot!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.