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. How to do Xor operation with QByteArray and copy it to another QBytearray[solved]
QtWS25 Last Chance

How to do Xor operation with QByteArray and copy it to another QBytearray[solved]

Scheduled Pinned Locked Moved General and Desktop
c++errorqbytearrayxor operation
12 Posts 3 Posters 7.0k Views
  • 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.
  • H Offline
    H Offline
    Huulivoide
    wrote on 22 Jun 2015, 13:27 last edited by Huulivoide
    #2

    Well first of all QByteArray m_oldData[68] creates an array of QByteArray objects.
    If you want a QbyteArray of spesific size you need to use the
    QByteArray(int size, char setEveryByteToThisChar) ctor instead.

    Also I don't see why you would need to XOR the bytes. A regular == comparison
    should do just fine.

    QByteArray old_data("initial data bytes");
    QByteArray new_data = my_socket->readAll();
    
    for (int i = 0; i < new_data.size(); ++i)
    {
        if ((i < old_data.size() && old_data[i] != new_data[i]) || i >= old_data.size())
            emit byteChanged(i, new_value[i]);
    }
    
    old_data = new_data;
    
    V 2 Replies Last reply 22 Jun 2015, 14:12
    1
    • H Huulivoide
      22 Jun 2015, 13:27

      Well first of all QByteArray m_oldData[68] creates an array of QByteArray objects.
      If you want a QbyteArray of spesific size you need to use the
      QByteArray(int size, char setEveryByteToThisChar) ctor instead.

      Also I don't see why you would need to XOR the bytes. A regular == comparison
      should do just fine.

      QByteArray old_data("initial data bytes");
      QByteArray new_data = my_socket->readAll();
      
      for (int i = 0; i < new_data.size(); ++i)
      {
          if ((i < old_data.size() && old_data[i] != new_data[i]) || i >= old_data.size())
              emit byteChanged(i, new_value[i]);
      }
      
      old_data = new_data;
      
      V Offline
      V Offline
      vishnu
      wrote on 22 Jun 2015, 14:12 last edited by
      #3

      @Huulivoide
      Thanks a lot for the reply. I need to access not only each byte. Each bit. I have updated the question again.Sorry for the incomplete question.
      I received 68byted of data from the socket looks like this:
      01000000000000000000000045020001000000000000000015ef0a0000000000d1f5070015ef0a0000000000d1f507007a77000000000000000000000000000000000000.
      Now i have to parse through each byte and then each bit. Then set the bool value of that particular bit to the particular button(true/false).

      1 Reply Last reply
      0
      • H Huulivoide
        22 Jun 2015, 13:27

        Well first of all QByteArray m_oldData[68] creates an array of QByteArray objects.
        If you want a QbyteArray of spesific size you need to use the
        QByteArray(int size, char setEveryByteToThisChar) ctor instead.

        Also I don't see why you would need to XOR the bytes. A regular == comparison
        should do just fine.

        QByteArray old_data("initial data bytes");
        QByteArray new_data = my_socket->readAll();
        
        for (int i = 0; i < new_data.size(); ++i)
        {
            if ((i < old_data.size() && old_data[i] != new_data[i]) || i >= old_data.size())
                emit byteChanged(i, new_value[i]);
        }
        
        old_data = new_data;
        
        V Offline
        V Offline
        vishnu
        wrote on 22 Jun 2015, 14:29 last edited by
        #4
        This post is deleted!
        H 1 Reply Last reply 22 Jun 2015, 14:52
        0
        • V vishnu
          22 Jun 2015, 14:29

          This post is deleted!

          H Offline
          H Offline
          Huulivoide
          wrote on 22 Jun 2015, 14:52 last edited by
          #5

          QByteArray old_data(86, '\0') should do it.

          if (new_data[i] & (1 << bitPosInByte))
          will evaluate true if the bit at offset bitPosInByte in the byte is set.

          1 Reply Last reply
          0
          • H Offline
            H Offline
            Huulivoide
            wrote on 22 Jun 2015, 14:57 last edited by
            #6

            No that doesn't work. You need to take the raw char sequence out of the QByteArray
            with data() function. Then you can test raw_bytes[i] & (1<<N)

            V 1 Reply Last reply 22 Jun 2015, 15:21
            0
            • H Huulivoide
              22 Jun 2015, 14:57

              No that doesn't work. You need to take the raw char sequence out of the QByteArray
              with data() function. Then you can test raw_bytes[i] & (1<<N)

              V Offline
              V Offline
              vishnu
              wrote on 22 Jun 2015, 15:21 last edited by
              #7

              @Huulivoide
              Sorry I didn't get the point.
              QByteArray old_data(68, '\0');
              QByteArray receivedData = m_tcpSocket->readAll();
              char *receivedDataptr = receivedData.data();// got raw data
              for(int i=0;i<receivedData.size();++i)
              {
              //what is the logic to compare old_data to new_data that gives the bit and byte index which is changed.
              for (int bitPosInByte=0; bitPosInByte<8;bitPosInByte++)
              {
              if (new_data[i] & (1 << bitPosInByte))// This is to get bit position
              }
              }
              //finally i copy
              old_data=receivedData ;

              Because next time I should come compare with my latest data bytes with old data . Is this what you want to say?

              1 Reply Last reply
              0
              • H Offline
                H Offline
                Huulivoide
                wrote on 22 Jun 2015, 15:36 last edited by
                #8

                Sorry I forgot the actual bit comparing.
                if ((old_data[i] & (1 << bitPosInByte)) != (new_data[i] & (1 << bitPosInByte)))

                That should do it. Does AND on the pesific bit and return true if the AND
                results with old and new data are differend.

                V 1 Reply Last reply 26 Jun 2015, 09:32
                1
                • H Huulivoide
                  22 Jun 2015, 15:36

                  Sorry I forgot the actual bit comparing.
                  if ((old_data[i] & (1 << bitPosInByte)) != (new_data[i] & (1 << bitPosInByte)))

                  That should do it. Does AND on the pesific bit and return true if the AND
                  results with old and new data are differend.

                  V Offline
                  V Offline
                  vishnu
                  wrote on 26 Jun 2015, 09:32 last edited by vishnu
                  #9

                  @Huulivoide
                  works perfect. But how to get the bool value? I mean whether it is true or not.
                  I tried like this but the value i am getting is in integers.
                  for eg :byte index 49 bit index 4 is changed value is 16
                  snippet:

                  QByteArray receivedData = m_tcpSocket->readAll();
                     for(int bytePos=0;bytePos<receivedData.size();++bytePos)
                      {
                          for(int bitPosInByte=0;bitPosInByte<8;++bitPosInByte)
                          {
                              if ((m_oldData[bytePos] & (1 << bitPosInByte)) != (receivedData[bytePos] & (1 << bitPosInByte)))
                                  qDebug()<<"byte index"<<bytePos<<"bit index"<<bitPosInByte <<"is changed"<<"value is "<<bool(receivedData[bytePos] & (1 << bitPosInByte));
                          }
                    }
                  
                  H 1 Reply Last reply 27 Jun 2015, 15:34
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 26 Jun 2015, 22:04 last edited by
                    #10

                    Working here, what OS/Qt/Compiler combo are you using ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    V 1 Reply Last reply 29 Jun 2015, 06:47
                    0
                    • V vishnu
                      26 Jun 2015, 09:32

                      @Huulivoide
                      works perfect. But how to get the bool value? I mean whether it is true or not.
                      I tried like this but the value i am getting is in integers.
                      for eg :byte index 49 bit index 4 is changed value is 16
                      snippet:

                      QByteArray receivedData = m_tcpSocket->readAll();
                         for(int bytePos=0;bytePos<receivedData.size();++bytePos)
                          {
                              for(int bitPosInByte=0;bitPosInByte<8;++bitPosInByte)
                              {
                                  if ((m_oldData[bytePos] & (1 << bitPosInByte)) != (receivedData[bytePos] & (1 << bitPosInByte)))
                                      qDebug()<<"byte index"<<bytePos<<"bit index"<<bitPosInByte <<"is changed"<<"value is "<<bool(receivedData[bytePos] & (1 << bitPosInByte));
                              }
                        }
                      
                      H Offline
                      H Offline
                      Huulivoide
                      wrote on 27 Jun 2015, 15:34 last edited by
                      #11

                      @vishnu
                      It is false if the the bitwise AND is 0, else it it true

                      1 Reply Last reply
                      0
                      • S SGaist
                        26 Jun 2015, 22:04

                        Working here, what OS/Qt/Compiler combo are you using ?

                        V Offline
                        V Offline
                        vishnu
                        wrote on 29 Jun 2015, 06:47 last edited by vishnu
                        #12

                        @SGaist
                        Windows 7 64bit/ Qt.5.4.1 /MinGw 32bit

                        1 Reply Last reply
                        0

                        11/12

                        27 Jun 2015, 15:34

                        • Login

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