[solved ] binary data to QTcpSocket
-
Please help :'(
I am reading a image example a.jpg by
@
QFile file("d:/a.jpg");
file.open(QIODevice::ReadOnly);
QTextStream st(&file);
@
when you extract info like st.readlAll();we get all the binary stuff like all unknow chars.
but
when i convert it to char * i.e ASCII by using ( st.readAll().toAscii() ) it is getting only 4 chars others are missing..
so can any please help me to convert entire QString i mean binary data of image to ASCII because the application i am working only supports ASCII please help.I tried as many possible combinations as possible but not working . !http://img547.imageshack.us/img547/6366/16772393.png(Before convertion to ASCII)!
!http://img35.imageshack.us/img35/1824/39767045.png(After i convert to ASCII)!
I am attaching images .
-
Hi,
Before digging deeper, why do you want/need to read a JPEG like this ?
-
Then, why not simply use QFile's "readAll()":http://qt-project.org/doc/qt-4.8/qiodevice.html#readAll ? It returns you a QByteArray with the data from the file
-
Don't convert to ASCII. A char* is going to give you trouble the first time it encounters 0x00 (NULL) which will be interpreted as the end of the string. (JPEGs will certainly have a number of NULL characters in them -- I bet the first one you're encountering is at position 5 in your data.) A QByteArray will be sufficient to hold your data. You shouldn't be using a QTextStream at all, also. As SGaist said, QFile::readAll() should get you what you want. However, if you were insistent on using a stream, you'd need to use a QDataStream instead.
How, exactly, are you checking your data?
-
Well of course it does. You need to realise that a JPEG file's content is not text and therefore any attempt to treat it is as such (e.g. with QTextStream or QTextEdit, notice the word Text) is doomed to fail. A JPEG binary file will typically have a zero in its fifth byte so you will generally get only four potential characters (byte FF D8 FF E0 in hex) before the string is considered ended. If byte 5 is not zero then the 11th byte will always be zero and you may see a recognisable "JFIF" in the text.
Why don't you take a step back and explain the result you are expecting to achieve.
-
As ChrisW67 said, JPEG is NOT a text file so QTextEdit won't be able to support it. If you want to show it as text, at least convert it using QByteArray::toHex(). But I don't think we are on the right track then.
-
@ QFile file("d:/a.jpg");
file.open(QIODevice::ReadOnly);
this->sock = new QTcpSocket;
connect(this->sock,SIGNAL(bytesWritten(qint64)),this,SLOT(q(qint64))); connect(sock,SIGNAL(connected()),this,SLOT(con())); connect(sock,SIGNAL(disconnected()),this,SLOT(dis())); this->sock->connectToHost("192.168.1.2",8080); this->sock->waitForConnected(); sock->write(file.readAll().data()); sock->waitForBytesWritten(-1); sock->close();
file.close();
@
This is my code i need to send the image via network using QTcpSocket but the method QTcpSocket::write() will only support char * or bytearray but even if i convert the above image from QString to QByteArry and pass to QTcpSocket and send it is only showing 4 characters in the both cases QByteArray and char *.Edit: again, use @ tags around code sections please; Andre
-
Don't convert anything, just send the byte array created by readAll().
Besides, write(file.readAll().data()) is a bad idea. data() will return a pointer to the data from a temporary QByteArray created by file.readAll(). You don't really know when that byte array will be destroyed.
If you want to show the image on the other end, use a "QImage":http://qt-project.org/doc/qt-4.8/qimage.html + "QLabel":http://qt-project.org/doc/qt-4.8/qlabel.html combo
And please enclose your code with code tags so that it's cleaner to read.
-
[quote author="S.ANAND" date="1363174874"] QTcpSocket::write() will only support char * or bytearray but even if i convert the above image from QString to QByteArry and pass to QTcpSocket and send it is only showing 4 characters in the both cases QByteArray and char *. [/quote]
It should work... The fact that you are using QTextEdit is NOT correct. QTextEdit only support "Texts".
-
You're welcome
If your issue is solved, please update the thread title and add [solved]