PDA

View Full Version : QDateTime to QString Hex to QByteArray - Problem (



f00rZik
17th June 2016, 15:45
ЗдравствуйтРµ, прошу прощения, что пиши по русски.
Возникла проблема.
ПреобразовыРаю QdateTime (unix) в Hex QString

QString time = QString("%1").arg(QDateTime::currentDateTime().toTime_t(),0,16 ).toUpper();
Всё окей.
После пытаюсь сделать

QByteArray myHexArray = QByteArray::fromHex(time.toLatin1());
но выходит какая-то лажа.
к примеру получил я HEX - 57640466
Если я вручную ввожу его

QByteArray myHexArray = QByteArray::fromHex("57640466");
То всё правильно происходит на выходе вижу

0x57 0x64 0x04 0x66
А если заместо значения подставляю time то на выходе получается hex обёрнутый ещё раз в hex
Как так я не понимаю.
Даже если со значениями всё ок, дальше почему-то
если беру отдельно индекс 0 у myHexArray получаю не 0x57, а
[0] = 0x05, [1] = 0x07
Помогите пожалуйста )

ChrisW67
18th June 2016, 13:25
Courtesy of Google Translate

Hello, I am sorry that write in Russian.
There is a problem.
Converts QdateTime (unix) in Hex QString

QString time = QString("%1").arg(QDateTime::currentDateTime().toTime_t(),0,16 ).toUpper();
All OK.
After trying to make

QByteArray myHexArray = QByteArray::fromHex(time.toLatin1());
but leaves some crap.
for example, I got HEX - 57640466
If I manually insert it

QByteArray myHexArray = QByteArray::fromHex("57640466");
That is the right thing happens at the output see

0x57 0x64 0x04 0x66
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
Просьба представить небольшую, полную программу, Ð´ÐµÐ¼Ð¾Ð½ÑÑ‚Ñ€Ð¸Ñ€ÑƒÑ Ñ‰ÑƒÑŽ проблему

f00rZik
20th June 2016, 08:41
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

array[0] = 0xAB
output array
2 key, rather than one

array[0] = 0x0A
array[1] = 0x0B

ChrisW67
20th June 2016, 09:29
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:


#include <QCoreApplication>
#include <QDateTime>
#include <QByteArray>
#include <QString>
#include <QDebug>

int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);

uint timet = QDateTime::currentDateTime().toTime_t();
qDebug() << "time_t value in hex: " << hex << timet;

QString time = QString("%1").arg(timet,0,16).toUpper();
QByteArray myHexArray = QByteArray::fromHex(time.toLatin1());
qDebug() << "time ==" << time;
qDebug() << "myHexArray is" << myHexArray.size() << "bytes";
qDebug() << "myHexArray == " << myHexArray.toHex();
return 0;
}

Output:


time_t value in hex: 5767a69b
time == "5767A69B"
myHexArray is 4 bytes
myHexArray == "5767a69b"

All looks good to me.