[solved ] binary data to QTcpSocket

  • Please help :'(

    I am reading a image example a.jpg by
    QFile file("d:/a.jpg");
    QTextStream st(&file);
    when you extract info like st.readlAll();

    we get all the binary stuff like all unknow chars.

    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 .

  • Lifetime Qt Champion


    Before digging deeper, why do you want/need to read a JPEG like this ?

  • hi.

    Actually i need to transfer jpeg image in a network using QtcpSocket so QTcpSocket::write() function only supports ascii or bytearray . that is the reason to convert to ASCII

  • Lifetime Qt Champion

    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

  • Hi

    That is not working i am getting the same output the second image one.

  • Moderators

    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?

  • Thats not working.

    I am checking like this
    QFile file("d:/a.jpg");
    It is giving second image output.

    edit: please use @ tags around code sections; Andre

  • 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");


    this->sock = new QTcpSocket;


    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

  • Lifetime Qt Champion

    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".

  • Ha that's done the trick.
    using file.readAll() instead of file.readAll().data()

    Thank You SGaist and all others you replied. :)

  • Yes that is also the mistake i made Code_ReaQtor Thank you .

  • Lifetime Qt Champion

    You're welcome

    If your issue is solved, please update the thread title and add [solved]

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.