PDA

View Full Version : PlEASE HELP: Hot To Use Byte Array, data stream



aash_89
16th July 2010, 04:17
Hey all.
this is part of my code

QDataStream data(&arr,QIODevice::ReadWrite);
data.setVersion(QDataStream::Qt_4_6);
data<<(quint16(0))<<quint8('S');
data<<message;
data<<(quint8('L'));
data.device()->seek(0);
data<<(arr.size()-sizeof(quint16));
data.device()->seek(0);
QString a;
quint8 type1,type2;
quint16 size;
data>>size>>type1>>a>>type2;
ui->textBrowser->append(QString::number(size));
ui->textBrowser->append(QString::number(type1));
ui->textBrowser->append(a);
ui->textBrowser->append(QString::number(type2));


message is a non empty QString;
arr is a ByteArray;

On the text browser i get the output as
0
0

0
Exactly as above.
what is happening to all the data im writing! why is it all showing 0??

Please help


Im actually writing this data block over a stream using TCPsocket, but i wasnt reciving any data at the other end, so i just did the above to see if data is being written at the source!!

saa7_go
16th July 2010, 05:18
Look at the detail description of QBuffer (http://doc.trolltech.com/4.6/qbuffer.html) class.

ChrisW67
16th July 2010, 06:55
QDataStream data(&arr,QIODevice::ReadWrite);
data.setVersion(QDataStream::Qt_4_6);
data<<(quint16(0))<<quint8('S');
data<<message;
data<<(quint8('L'));
data.device()->seek(0);
data<<(arr.size()-sizeof(quint16)); // How many bytes are being written here?
tBrowser->append(QString::number(type2));


When I try a quick test:

QString message("ABCDEF");
QByteArray arr;

QDataStream data(&arr,QIODevice::ReadWrite);
data.setVersion(QDataStream::Qt_4_6);
data<< quint16(0xBEEF) <<quint8('S');
data<<message;
data<<(quint8('L'));
qDebug() << arr.toHex();
data.device()->seek(0);
data << (arr.size() - sizeof(quint16));
qDebug() << arr.toHex();
data.device()->seek(0);

I get 18 bytes (0x00000012 bytes) excluding the size field:


"beef530000000c0041004200430044004500464c"
"0000001200000c0041004200430044004500464c"

I think you can see the problem now.

PS: Were you expecting Unicode characters at the receiving end?

aash_89
16th July 2010, 07:19
Thank you Chris for your reply!!

Im really new to Qt development, 1 week into it.

I wrote a client server application, the the code snippet i pasted was from the client side. i was trying to send data to the server using QTcpSocket.

The socket was conncted sucessfully, problem was with the data! so i wanted to see if data is being written to the Bytearray or not by writing data to it and reading from it and displaying it on a text browser.

the byte array is a block of data i want to send.

the first one is the size of the data block, since i dont know the size of the wole block i initially write it to 0, the next is a character to 'S' signifying start of the data block. the next block is the data i want to be sending, its a QString now, but later want to be able to send images. the last block is the charater 'L' signifying end of the block.

after wrtiting the end, i calculate the size of the QByteArray, then use seek(0) to go to the first block, and rewrite the size.


and finally send this packet of information.

but when i read out of it, i just get null values.

I dont understand why.

Please forgive my inexperience.

thank you
aashish

ChrisW67
16th July 2010, 07:37
Take a close look at the first four bytes (8 characters) of the hex dumps: the first is before the size is updated, the second after. I put 0xBEEF into the size as a default value to make it more obvious. You allocate two bytes to hold the size, but write four bytes when you store the size in the data stream. Look at what happens when I write the size the way you did.

0x53 is 'S'
0x0041, 0x0042 ... 0x0046 are the letters ABCDEF as unicode
0x4c is 'L'.

aash_89
16th July 2010, 07:58
1."beef530000000c0041004200430044004500464c"
2."0000001200000c0041004200430044004500464c"

Thank you, i understand now.

If the size is 18, 2 bytes is enough to represent it right?
why is it taking 4 bytes? how can i make it take 2 bytes only.

what i mean to ask is, the size is being represented as 00000012, why not just 12.

also after 's' there is "0000000c" 4 bytes of you did not write but its there!


i was under the impression that, the bytes should look like this

120c4142434445464c

as each char of "ABCDEF" should take 1 byte.

am I not understanding something correctly here?

thank you for your guidance.

aash_89
16th July 2010, 08:34
many thnakx chris for your help.
i just size constrained the last line and it works.

ChrisW67
16th July 2010, 09:33
The QDataStream writes enough information to reconstruct the item being written. When you write a byte, short, or int you get exactly 1, 2, or 4 bytes. Strings are written with an int indicating the length of the following string, in my example this is 12 bytes (6 x 2bytes per Unicode character). If you want single byte chars you should convert the QString to a char *, but you will still get the leading int indicating size.


QDataStream data(&arr,QIODevice::ReadWrite);
data.setVersion(QDataStream::Qt_4_6);
data<< quint16(0xBEEF) <<quint8('S');
data<< message.toAscii();
data<<(quint8('L'));
qDebug() << arr.toHex();
data.device()->seek(0);
data << quint16(arr.size() - sizeof(quint16));
qDebug() << arr.toHex();
data.device()->seek(0);

...

"beef53000000064142434445464c"
"000c53000000064142434445464c"


If you want absolute control of the bytes in the stream you probably don't want QDataStream; look into QBuffer and QByteArray