PDA

View Full Version : Passing Specific Binary Characters in QTcpSocket.



nbkhwjm
1st September 2007, 01:51
I have this to write some data to a socket.


QByteArray data;
QDataStream out(&data, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_1);
out << int(0x02) << QString("text");
tcpSocket->write(data);
The Socket connects perfectly, and the data is sent however the results seen at the receiving end are not what i expected... I know there is a simple fundamental problem with the way I am defining the data to send.. i just cant see it..

I want to see this on the other end. It can only be this since this is a tool to integrate to an existing protocol.


0x02 74 65 78 74 0A

Which is [0x02]text[0x0A]


But I keep getting this..

0x00 00 00 02 00 00 00 08 00 74 00 65 00 78 00 74

[0x00 00 00 02 00 00 00 08 00]t[0x00]e[0x00]x[0x00]t

marcel
1st September 2007, 02:56
That is because you write unicode characters to the data stream.
What you need is to write ascii characters:


QString s = "text";
QByteArray b = s.toAscii();
const char* ascii = b.constData();
out << int(0x02) << ascii;



Regards

nbkhwjm
2nd September 2007, 17:15
That did indeed fix the double byte characters in the QString.. however i still have many additional bytes where on the 0x02 should be...

So, i made a test..

with this code:

QByteArray data;
QDataStream out(&data, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_1);
out << int(0x02);
tcpSocket->write(data);

I get this on the network..
0x00000002

What I need to see is :
0x02

Is there a different way to define this instead of int(0x02) to make do only this hex character? and not the additional nulls?

Thanks for the assistance...

marcel
2nd September 2007, 17:21
But you want to see 0x02(8 bits - 1 byte) and still you write an integer(32 bits - 4bytes).
Write a qint8 instead:


out << qint8(0x02);
Regards

nbkhwjm
2nd September 2007, 19:48
Thanks marcel,

yes, that was a fundamental mistake... works great now..