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. Converting qint8 to string
Forum Updated to NodeBB v4.3 + New Features

Converting qint8 to string

Scheduled Pinned Locked Moved General and Desktop
17 Posts 4 Posters 11.8k Views 1 Watching
  • 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.
  • R Offline
    R Offline
    roboterprogrammer
    wrote on last edited by
    #1

    hello,

    i want to send an qint8 via tcpip, and for that, after the connection is etablished via the socket,
    i want to use the
    @
    socket->write("GUISTATUS: \n");@
    directive
    and i want to add one qint8 variable, in which 8 independent informations are included, in each bit one.
    how can i put it in the string?

    it didnt work for me with QDataStream, i think because i didnt use a file.

    Thanks for hinds and advices.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      QDataStream should work just fine. You could prepare your data like this:
      @
      //pseudo-code, brain to terminal
      QByteArray data;
      QDataStream s(&data);
      s << "GUISTATUS\n";
      s << myUint8Variable;
      socket->write(data);
      @

      1 Reply Last reply
      0
      • R Offline
        R Offline
        roboterprogrammer
        wrote on last edited by
        #3

        hi,

        thanks for the fast reply.

        i tried

        @ QByteArray data;
        QDataStream s(&data);

            s << guistatus;
            s << "GUISTATUS\n";
            socke->write(data);@
        

        and i get an error:

        @C:\Dokumente und Einstellungen\loetrobo\Robbie20-build-desktop-Qt_4_8_0_for_Desktop_-_MinGW__Qt_SDK__Debug..\Desktop\Robbie20\robbie20.cpp:234: error: no matching function for call to 'QDataStream::QDataStream(QByteArray*)'@

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          According to the docs for [[doc:QDataStream]], there is a constructor for a QDataStream that takes a QByteArray pointer and a flag to indicate the opening mode, and there is a constructor that takes a QByteArray reference. The documentation explains why you want the first one when writing to the byte array.

          That's why I said "pseudo-code, brain to terminal". You're supposed to interpret that as "this is the general idea, lookup the exact function names and signatures yourself in the documentation".

          1 Reply Last reply
          0
          • K Offline
            K Offline
            KA51O
            wrote on last edited by
            #5

            This should solve the compiler error:

            @
            QDataStream s(&data, QIODevice::ReadWrite);
            @

            1 Reply Last reply
            0
            • R Offline
              R Offline
              roboterprogrammer
              wrote on last edited by
              #6

              hi, thanks thats what i tried

              thats my code:
              @ qint8 integ = 0b00001111;
              QByteArray helparray = "GUISTATUS\n";
              QByteArray data;
              QDataStream s(&data, QIODevice::ReadWrite);
              QString helpstring;

              s << helparray;
              s >> helpstring;
              qDebug() << "helpstring: " << helpstring;@

              i expected to get in my qdebug window, but the QString is totally empty.

              @helpstring: "" @

              where is my mistake?
              i cant find it, didn't know this conversation is such a big deal.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                On line 8 of your code above, you start reading at the current position in the stream. That is: after what you just wrote.

                Just use the write-only mode instead, and display your data bytearray directly with qDebug.

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  roboterprogrammer
                  wrote on last edited by
                  #8

                  hi,

                  ah okay. but when i do like this, i get an " QIODevice::read: WriteOnly device" advice.

                  @ qint8 integ = 0b00001111;
                  QByteArray helparray = "GUISTATUS";
                  QByteArray data;
                  QDataStream s(&data, QIODevice::WriteOnly);
                  QString helpstring;

                  s << integ <<" test";
                  qDebug() << "data: " << data;
                  s >> helpstring;

                    qDebug() << "helpstring: " <<  helpstring; @
                  

                  the output in the qdebug is not correct.
                  data: " -> incomplete
                  helpstring: "" -> empty

                  its important its a string i send, because then i can check if there are unwanted signs inside like /n i could remove manually.
                  i feel like qt put some of those with the << operator, so i keep full control.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

                    Hmmm.... Perhaps QDataStream isn't for you after all. I am sorry I suggested it. QDataStream does not only stream the raw data, but also some meta data (like the length of a QByteArray you stream in) if needed. You can circumvent that using QDataStream::writeBytes or QDataStream::writeRawBytes, but that defeats the point of using it in the first place, right?

                    Note that your warning is caused by your line 9 above. If you use a WriteOnly stream, you can't obviouly read from it. Also, streaming into a QString probably doesn't do what you expect it does... Instead, use one of the QString::from<Encoding>() methods to construct a string from a QByteArray.

                    Anyway, you can probably get rid of QDataStream completely, as it screws up your data in your case if you add a QByteArray. Instead, you could try this:

                    @
                    //again, untested code:
                    QByteArray data;
                    data += "GUISTATUS";
                    data.append( reinterpret_cast<char*>(&myUInt8Variable), 1 ); //Warning: no idea if this will work ok
                    qDebug() << "data:" << data;
                    @

                    Or, using QDataStream again, but only for the integer:
                    @
                    //again, untested code:
                    QByteArray data;
                    data += "GUISTATUS";
                    QDataStream s(&data, QIODevice::WriteOnly);
                    s << myUInt8Variable;
                    qDebug() << "data:" << data;
                    @

                    1 Reply Last reply
                    0
                    • N Offline
                      N Offline
                      Neutron Stein
                      wrote on last edited by
                      #10

                      did you try QString().setNum(myUInt8Variable) to convert ?

                      Never Seen !

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andre
                        wrote on last edited by
                        #11

                        [quote author="Neutron Stein" date="1342534822"]did you try QString().setNum(myUInt8Variable) to convert ?[/quote]

                        He was looking for a QByteArray (to send over a TCP socket). Converting to a string does not help for that.

                        1 Reply Last reply
                        0
                        • N Offline
                          N Offline
                          Neutron Stein
                          wrote on last edited by
                          #12

                          isn't the topic?converting from quint8 to string?

                          Never Seen !

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            andre
                            wrote on last edited by
                            #13

                            [quote author="Neutron Stein" date="1342535272"]isn't the topic?converting from quint8 to string?[/quote]

                            Not really:
                            [quote author="roboterprogrammer" date="1342524761"]i want to send an qint8 via tcpip, and for that, after the connection is etablished via the socket,
                            i want to use the
                            @
                            socket->write("GUISTATUS: \n");@
                            directive
                            and i want to add one qint8 variable, in which 8 independent informations are included, in each bit one.[/quote]

                            You need to read more than just the subject header... And yes, I sometimes fail to do that as well.

                            1 Reply Last reply
                            0
                            • N Offline
                              N Offline
                              Neutron Stein
                              wrote on last edited by
                              #14

                              I think this can help.
                              @
                              QByteArray paquet;
                              QDataStream data(&paquet,QIODevice::WriteOnly);
                              data << (quint8)0;
                              data << theStringToSend;
                              data.device()->seek(0);
                              data << (quint8)(paquet.size() - sizeof(quint8));
                              socket->write(paquet);
                              @

                              Never Seen !

                              1 Reply Last reply
                              0
                              • R Offline
                                R Offline
                                roboterprogrammer
                                wrote on last edited by
                                #15

                                hi,

                                solution 2 works now, so i can send data (changes that operator made) to gui,
                                but its not nice.

                                if no better solution i will keep it like this.
                                the thing is:

                                with @s << myUInt8Variable;@

                                it writes it to the first postiton of data.

                                so in qdebug instead of GUISTATUSX i get XUISTATUS

                                how can i put it on the end?

                                1 Reply Last reply
                                0
                                • R Offline
                                  R Offline
                                  roboterprogrammer
                                  wrote on last edited by
                                  #16

                                  wow so many replys i will read them, and try the new stuff out.

                                  thanks

                                  1 Reply Last reply
                                  0
                                  • N Offline
                                    N Offline
                                    Neutron Stein
                                    wrote on last edited by
                                    #17

                                    sorry I sent the wrong code.but it might be something like that. there i put the packet size at the beginning of the packet to be sent.i think you just need to put there what you want to send.
                                    Sorry for mistakes i'm newbie

                                    Never Seen !

                                    1 Reply Last reply
                                    0

                                    • Login

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