Results 1 to 4 of 4

Thread: QDateTime to QString Hex to QByteArray - Problem (

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2016
    Posts
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default QDateTime to QString Hex to QByteArray - Problem (

    ЗдравствуйтРµ, прошу прощения, что пиши по русски.
    Возникла проблема.
    ПреобразовыРаю QdateTime (unix) в Hex QString
    Qt Code:
    1. QString time = QString("%1").arg(QDateTime::currentDateTime().toTime_t(),0,16).toUpper();
    To copy to clipboard, switch view to plain text mode 
    Всё окей.
    После пытаюсь сделать
    Qt Code:
    1. QByteArray myHexArray = QByteArray::fromHex(time.toLatin1());
    To copy to clipboard, switch view to plain text mode 
    но выходит какая-то лажа.
    к примеру получил я HEX - 57640466
    Если я вручную ввожу его
    Qt Code:
    1. QByteArray myHexArray = QByteArray::fromHex("57640466");
    To copy to clipboard, switch view to plain text mode 
    То всё правильно происходит на выходе вижу
    Qt Code:
    1. 0x57 0x64 0x04 0x66
    To copy to clipboard, switch view to plain text mode 
    А если заместо значения подставляю time то на выходе получается hex обёрнутый ещё раз в hex
    Как так я не понимаю.
    Даже если со значениями всё ок, дальше почему-то
    если беру отдельно индекс 0 у myHexArray получаю не 0x57, а
    [0] = 0x05, [1] = 0x07
    Помогите пожалуйста )

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QDateTime to QString Hex to QByteArray - Problem (

    Courtesy of Google Translate
    Quote Originally Posted by f00rZik View Post
    Hello, I am sorry that write in Russian.
    There is a problem.
    Converts QdateTime (unix) in Hex QString
    Qt Code:
    1. QString time = QString("%1").arg(QDateTime::currentDateTime().toTime_t(),0,16).toUpper();
    To copy to clipboard, switch view to plain text mode 
    All OK.
    After trying to make
    Qt Code:
    1. QByteArray myHexArray = QByteArray::fromHex(time.toLatin1());
    To copy to clipboard, switch view to plain text mode 
    but leaves some crap.
    for example, I got HEX - 57640466
    If I manually insert it
    Qt Code:
    1. QByteArray myHexArray = QByteArray::fromHex("57640466");
    To copy to clipboard, switch view to plain text mode 
    That is the right thing happens at the output see
    Qt Code:
    1. 0x57 0x64 0x04 0x66
    To copy to clipboard, switch view to plain text mode 
    And if instead of the time value is inserted in the hex output is wrapped again in hex
    How so I do not understand.
    Even if everything is OK with the values, further reason
    if you take a separate index from 0 myHexArray not get 0x57, and
    [0] = 0x05, [1] = 0x07
    Help me please )
    Also from Google Translate:
    Please provide small, complete program that demonstrates the problem
    Просьба представить небольшую, полную программу, Ð´ÐµÐ¼Ð¾Ð½ÑÑ‚Ñ€Ð¸Ñ€ÑƒÑ Ñ‰ÑƒÑŽ проблему

  3. #3
    Join Date
    Jun 2016
    Posts
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDateTime to QString Hex to QByteArray - Problem (

    Quote Originally Posted by ChrisW67 View Post
    Courtesy of Google Translate

    Also from Google Translate:
    Please provide small, complete program that demonstrates the problem
    Просьба представить небольшую, полную программу, Ð´ÐµÐ¼Ð¾Ð½ÑÑ‚Ñ€Ð¸Ñ€ÑƒÑ Ñ‰ÑƒÑŽ проблему
    I see no reason to provide a demo version of the program.
    There's a protocol.
    In fact I described the problem seems to be normal, the problem is in the standard functions and it is not clear why instead of the array
    Qt Code:
    1. array[0] = 0xAB
    To copy to clipboard, switch view to plain text mode 
    output array
    2 key, rather than one
    Qt Code:
    1. array[0] = 0x0A
    2. array[1] = 0x0B
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QDateTime to QString Hex to QByteArray - Problem (

    The reason I asked for a example that produces the problem is because you fail to describe the problem.
    Your first two pieces of code will give you a four byte QByteArray that you says "leaves some crap" (or something like it). You do not explain or show what that crap might be.
    The rest of your post manually builds an unrelated 4 byte QByteArray and demonstrates that the array contain four bytes.

    Here is the example:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QDateTime>
    3. #include <QByteArray>
    4. #include <QString>
    5. #include <QDebug>
    6.  
    7. int main(int argc, char **argv)
    8. {
    9. QCoreApplication app(argc, argv);
    10.  
    11. uint timet = QDateTime::currentDateTime().toTime_t();
    12. qDebug() << "time_t value in hex: " << hex << timet;
    13.  
    14. QString time = QString("%1").arg(timet,0,16).toUpper();
    15. QByteArray myHexArray = QByteArray::fromHex(time.toLatin1());
    16. qDebug() << "time ==" << time;
    17. qDebug() << "myHexArray is" << myHexArray.size() << "bytes";
    18. qDebug() << "myHexArray == " << myHexArray.toHex();
    19. return 0;
    20. }
    To copy to clipboard, switch view to plain text mode 
    Output:
    Qt Code:
    1. time_t value in hex: 5767a69b
    2. time == "5767A69B"
    3. myHexArray is 4 bytes
    4. myHexArray == "5767a69b"
    To copy to clipboard, switch view to plain text mode 
    All looks good to me.
    Last edited by ChrisW67; 20th June 2016 at 09:44.

Similar Threads

  1. QByteArray to Qstring
    By bmps in forum Qt Programming
    Replies: 6
    Last Post: 16th November 2011, 20:02
  2. Problem with QString and QByteArray
    By ts66 in forum Newbie
    Replies: 2
    Last Post: 10th May 2011, 21:30
  3. How to Compare two QString in the form of QDateTime ?
    By George Neil in forum Qt Programming
    Replies: 4
    Last Post: 1st March 2010, 06:57
  4. Initialize QDateTime by a QString
    By mattia in forum Newbie
    Replies: 2
    Last Post: 14th March 2008, 13:42
  5. Replies: 1
    Last Post: 30th June 2006, 05:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.