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. QTcpSocket and QByteArray problem!
QtWS25 Last Chance

QTcpSocket and QByteArray problem!

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 4.1k 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.
  • R Offline
    R Offline
    roteiro
    wrote on last edited by
    #1

    Hello everybody, i have a problem with wich i struggled for the last 2-3 days. I have a simple Server application in wich i get data in a QByteArray variable i use the next line for this:

    QByteArray Data = socket->readAll();

    Then i output Data in hex format, to see that if what i get is the same with what the client sends me for this i do this:

    qDebug() << " Data in: " << Data.toHex();

    The output is this:

    "a55a55aa010000000000000064000000028d0000b63d795100000000410100000000db6900000000000000000000000000000000000000008e00040000000000000000000003000000000300000000000001"

    It's exactly what i was expecting, so far so good, somewhere in this string i have three bytes wich gives me local time in UTC format starting from position 20 these are the values that i need: 63d795. So i figure that in order to acces those variables i'll do this
    utcTime = Data [23]
    utcTime = utcTime << 8;
    utcTime = utcTime + Data [22];
    utcTime = utcTime << 8;
    utcTime = utcTime + Data [21];
    utcTime = utcTime << 8;
    utcTime = utcTime + Data [20];

    then i do this in order to format the date and time

    QDateTime localTime(QDate(), QTime(), Qt::UTC);
    localTime.setTime_t(abs(tempUtcTime));
    qDebug() << localTime;

    The clock shows the correct time at some point but when the third member respectivley Data [20] reaches 128 (0x80) it then changes the sign and starts counting -127...-126.....when it reaches 0 it works normally untill again reaches 128. I'm really blocked, i don't know what to do further please help me

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      is client and server written in Qt?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

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

        No the client is something else CodeSys is an industrial programming language or something like that ..., the server is written in Qt

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi and welcome do DevNet,

          QByteArray has it names indicates work with byte. Signed bytes go from -127 to 128.

          You have to parse your byte array differently or send your data in a more practical format.

          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
          • R Offline
            R Offline
            roteiro
            wrote on last edited by
            #5

            I know, but i don't really know how to do the parsing another way, thank you

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Could you show us the exact format of your data ? I mean, what the server sends and what value is it supposed to be.

              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
              • R Offline
                R Offline
                roteiro
                wrote on last edited by
                #7

                This is my function:
                @void MyThread::readyRead()
                {
                quint32 utcTime;

                QByteArray Data = socket->readAll();
                QByteArray stringDataOra;
                qDebug() << " Data in: " << Data.toHex();
                
                
                utcTime = Data[23];
                utcTime = (utcTime << 8);
                utcTime = utcTime + Data[22];
                utcTime = (utcTime << 8);
                utcTime = utcTime + Data[21];
                utcTime = (utcTime << 8);
                utcTime = utcTime + Data[20];
                
                QDateTime localTime(QDate(), QTime(), Qt::UTC);
                localTime.setTime_t(abs(tempUtcTime));
                qDebug() << localTime;
                

                }@

                This is the QByteArray:
                "a55a55aa010000000000000064000000028d0000b63d795100000000410100000000db6900000000000000000000000000000000000000008e00040000000000000000000003000000000300000000000001"

                and from this array i try to extract the bolded bytes

                but in reverse order 51793db6

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  That i understood, what I want to know is: what does 51793db6 represent ? The time as in hh.MM.ss.zzz ? A time in seconds or milliseconds ? Others ?

                  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
                  • R Offline
                    R Offline
                    roteiro
                    wrote on last edited by
                    #9

                    Exactly represents the time given in seconds in UTC format yyyy.mm.dd hh:mm:ss

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      roteiro
                      wrote on last edited by
                      #10

                      The output was this "Thu Apr 25 14:27:22 2013" it incraments the seconds correct till a point respectivly Data [20] becomes 128 then the seconds start to decrement till Data [20] is 0 again, then it upgrades the time...and start working normally and so on...

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        In that case:
                        Why not use "QByteArray::mid":http://qt-project.org/doc/qt-4.8/qbytearray.html#mid to retrieve the value then "QByteArray::toInt":http://qt-project.org/doc/qt-4.8/qbytearray.html#toInt to have it as an int and finally "QDateTime::formTime_t":http://qt-project.org/doc/qt-4.8/qdatetime.html#fromTime_t ? to make your local time ?

                        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

                        • Login

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