Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. One letter string into char and then bit-wise comparing
Forum Updated to NodeBB v4.3 + New Features

One letter string into char and then bit-wise comparing

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 3.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    roboterprogrammer
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tucnak
      wrote on last edited by
      #2

      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

      @

      1 Reply Last reply
      0
      • S Offline
        S Offline
        shanek
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          roboterprogrammer
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            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
            }
            @

            1 Reply Last reply
            0
            • R Offline
              R Offline
              roboterprogrammer
              wrote on last edited by
              #6

              thanks, that helped a lot!

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved