Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved ] binary data to QTcpSocket
QtWS25 Last Chance

[solved ] binary data to QTcpSocket

Scheduled Pinned Locked Moved General and Desktop
15 Posts 5 Posters 10.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    S.ANAND
    wrote on 12 Mar 2013, 16:40 last edited by
    #1

    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 .

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 12 Mar 2013, 16:57 last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        S.ANAND
        wrote on 12 Mar 2013, 17:13 last edited by
        #3

        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

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 12 Mar 2013, 17:20 last edited by
          #4

          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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • S Offline
            S Offline
            S.ANAND
            wrote on 13 Mar 2013, 00:35 last edited by
            #5

            Hi

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

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mlong
              wrote on 13 Mar 2013, 00:47 last edited by
              #6

              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?

              Software Engineer
              My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                S.ANAND
                wrote on 13 Mar 2013, 01:06 last edited by
                #7

                Thats not working.

                I am checking like this
                @
                QFile file("d:/a.jpg");
                file.open(QIODevice::ReadOnly);
                ui->textEdit->append(file.readAll());
                file.close();
                @
                It is giving second image output.

                edit: please use @ tags around code sections; Andre

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  ChrisW67
                  wrote on 13 Mar 2013, 07:00 last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    Code_ReaQtor
                    wrote on 13 Mar 2013, 07:48 last edited by
                    #9

                    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.

                    Please visit my open-source projects at https://github.com/Code-ReaQtor.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      S.ANAND
                      wrote on 13 Mar 2013, 11:41 last edited by
                      #10

                      @ 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

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 13 Mar 2013, 11:56 last edited by
                        #11

                        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.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • C Offline
                          C Offline
                          Code_ReaQtor
                          wrote on 13 Mar 2013, 11:59 last edited by
                          #12

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

                          Please visit my open-source projects at https://github.com/Code-ReaQtor.

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            S.ANAND
                            wrote on 13 Mar 2013, 12:04 last edited by
                            #13

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

                            Thank You SGaist and all others you replied. :)

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              S.ANAND
                              wrote on 13 Mar 2013, 12:05 last edited by
                              #14

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

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on 13 Mar 2013, 13:28 last edited by
                                #15

                                You're welcome

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

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                0

                                5/15

                                13 Mar 2013, 00:35

                                topic:navigator.unread, 10
                                • Login

                                • Login or register to search.
                                5 out of 15
                                • First post
                                  5/15
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved