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.
  • V Offline
    V Offline
    vishnu
    wrote on 22 Jun 2015, 12:17 last edited by vishnu
    #1

    I have received Hex data from the socket. Now I want to update the GUI based on the data changed. So my logic is to perform XOR operation between newData and oldData so that I get the Byte which is changed .Then again i parse through that byte to get which bit is changed.Then I want to send byte index,bit index and value to QML through signal(byteindex,bitindex,value). As the first step to compared each byte I am getting error.Also Can you please tell me how to copy one Qbytearray to another one( receviedData to m_oldData).Can any one please help me.Thanks

    QByteArray m_oldData(68,'\0');//How to Initialize all bytes to zero.I know i will get 68 bytes from the socket.
    QByteArray receivedData = m_tcpSocket->readAll();
     
    for(int i=0;i<receivedData.size();++i)
        {
            char result = (receivedData.at(i)^(m_oldData.at(i)));
            qDebug()<<"result "<<result;
            if(result!=0)//means byte is changed
            {
                //To know which bit is changed, this logic is not working. don't know why :(
                for(int j=0;j<8;++j)
                {
                   if(result & (1<<j))
                   {
                        qDebug()<<"byte index"<<i<<"bit index"<<j <<"is changed";
                        //Now send i and j value to QML.
                   }
                }
            }
        }
    

    //How to copy Qbytearray
    m_oldData = receivedData//not working.Because of specifying the size of m_oldData. If I don't specify it works. Then How to initialize all the bytes of m_oldData to zero?

                           ^
    
    1 Reply Last reply
    0
    • 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

                          4/12

                          22 Jun 2015, 14:29

                          topic:navigator.unread, 8
                          • Login

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