PDA

View Full Version : Bytes help again D:



Niunzin
6th January 2014, 16:48
Hi, i'm new in qt and i don't know how to send bytes D:

I want to follow this protocol:
C: 04
CC: 04
Name: THREAD_REPLY
Format (Sent): thread_id (int), message [short, string]

I tried it, but doesnt worked:

QByteArray reply;
reply[0] = 0x00;
reply[1] = 0x00;
reply[2] = 0x00;
reply[3] = 0x2f;
reply[4] = 0x04;
reply[5] = 0x04;
reply[6] = 0x00;
reply[7] = 572436;
reply[8] = 0x00;
reply[9] = 0x00;
reply[10] = 0x00;
reply[11] = ui->lineEdit->text().toUtf8();

sendPacket(reply);
http://img560.imageshack.us/img560/2060/6k72.png

Thanks!

ChrisW67
6th January 2014, 20:56
What about it doesn't work? We are not mind readers.

Some general observations:

If the packet is supposed to be a four byte int (assumed) followed by a two byte short indicating the length of the following string then the list of bytes you have there is too long.
Multi byte integers are sensitive to byte order issues. Make sure sender and receiver agree.
Line 9 makes no sense. You cannot put that number in a single byte.
Line 11 tries to put an entire string in a single byte. Did you perhaps intend to append the string?

Niunzin
6th January 2014, 21:42
I need to convert an String to Hex (like J -> 6a) and add it to the byte array D:

zaphod.b
6th January 2014, 22:09
Have a look at QByteArray::toHex().

ChrisW67
6th January 2014, 22:15
No, you want to append the bytes of the string to the bytes already in the array. You are part way there with QString::toUtf8() (which gives you an eight-bit encoded string in a byte array) but you want to use QByteArray::append() or QByteArray::operator+().