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



  • 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?

                           ^


  • 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;


  • @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).



  • This post is deleted!


  • 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.



  • 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)



  • @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?



  • 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.



  • @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));
            }
      }
    

  • Lifetime Qt Champion

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



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



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


Log in to reply
 

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