How to do Xor operation with QByteArray and copy it to another QBytearray[solved]
-
wrote on 22 Jun 2015, 13:27 last edited by 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;
-
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;
wrote on 22 Jun 2015, 14:12 last edited by@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). -
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;
wrote on 22 Jun 2015, 14:29 last edited byThis post is deleted! -
wrote on 22 Jun 2015, 14:52 last edited by
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. -
wrote on 22 Jun 2015, 14:57 last edited by
No that doesn't work. You need to take the raw char sequence out of the QByteArray
with data() function. Then you can testraw_bytes[i] & (1<<N)
-
No that doesn't work. You need to take the raw char sequence out of the QByteArray
with data() function. Then you can testraw_bytes[i] & (1<<N)
wrote on 22 Jun 2015, 15:21 last edited by@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?
-
wrote on 22 Jun 2015, 15:36 last edited by
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. -
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.wrote on 26 Jun 2015, 09:32 last edited by 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)); } }
-
Working here, what OS/Qt/Compiler combo are you using ?
-
@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)); } }
wrote on 27 Jun 2015, 15:34 last edited by@vishnu
It is false if the the bitwise AND is 0, else it it true
11/12