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]
Qt 6.11 is out! See what's new in the release blog

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 10.4k Views 2 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.
  • H Offline
    H Offline
    Huulivoide
    wrote on 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;
    
    vishnuV 2 Replies Last reply
    1
    • H Huulivoide

      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;
      
      vishnuV Offline
      vishnuV Offline
      vishnu
      wrote on 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

        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;
        
        vishnuV Offline
        vishnuV Offline
        vishnu
        wrote on last edited by
        #4
        This post is deleted!
        H 1 Reply Last reply
        0
        • vishnuV vishnu

          This post is deleted!

          H Offline
          H Offline
          Huulivoide
          wrote on 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 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)

            vishnuV 1 Reply Last reply
            0
            • H Huulivoide

              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)

              vishnuV Offline
              vishnuV Offline
              vishnu
              wrote on 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 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.

                vishnuV 1 Reply Last reply
                1
                • H Huulivoide

                  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.

                  vishnuV Offline
                  vishnuV Offline
                  vishnu
                  wrote on 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
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 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

                    vishnuV 1 Reply Last reply
                    0
                    • vishnuV vishnu

                      @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 last edited by
                      #11

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

                      1 Reply Last reply
                      0
                      • SGaistS SGaist

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

                        vishnuV Offline
                        vishnuV Offline
                        vishnu
                        wrote on last edited by vishnu
                        #12

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

                        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