QTcpSocket and QByteArray problem!



  • 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


  • Moderators

    is client and server written in Qt?



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


  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

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



  • 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


  • Lifetime Qt Champion

    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 ?



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



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


  • Lifetime Qt Champion

    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 ?


Log in to reply
 

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