Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    QTcpSocket and QByteArray problem!

    General and Desktop
    3
    11
    3502
    Loading More Posts
    • 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
      roteiro last edited by

      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 Reply Quote 0
      • raven-worx
        raven-worx Moderators last edited by

        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 Reply Quote 0
        • R
          roteiro last edited by

          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 Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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 Reply Quote 0
            • R
              roteiro last edited by

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

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                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 Reply Quote 0
                • R
                  roteiro last edited by

                  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 Reply Quote 0
                  • SGaist
                    SGaist Lifetime Qt Champion last edited by

                    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 Reply Quote 0
                    • R
                      roteiro last edited by

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

                      1 Reply Last reply Reply Quote 0
                      • R
                        roteiro last edited by

                        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 Reply Quote 0
                        • SGaist
                          SGaist Lifetime Qt Champion last edited by

                          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 Reply Quote 0
                          • First post
                            Last post