Reputation: 5102
I'm trying to convert a QByteArray
that contains a value in hex format to long long
:
QByteArray field = "A8BC301F0193E83F";
bool ok;
qlonglong val = field.toLongLong(&ok, 16);
qDebug() << ok << val;
output:
false 0
Instead I'm expecting val = -6.288.098.069.987.465.153.
Why it fails? The docs says nothing about any limitation of this function.
I forgot to mention the data order is little endian in the source.
Upvotes: 1
Views: 489
Reputation: 497
You can find the final function Qt call to convert ByteArray
to long long
here qlocale.cpp in function static qlonglong qstrtoll
It defines
cutoff = neg ? qulonglong(0-(LLONG_MIN + LLONG_MAX)) + LLONG_MAX : LLONG_MAX;
.
So cutoff
will be 0x8000000000000000
if neg
, or will be 0x7fffffffffffffff
if non neg
.
Your input value is bigger than cutoff
so it returns false
.
You can get the static qlonglong qstrtoll
function and debug by your own.
Upvotes: 1
Reputation: 11575
Your number exceeds the limits of a signed long long
, so conversion fails.
You can use QByteArray::toULongLong
instead and then make a cast to long long
.
#include <qbytearray.h>
#include <qdebug.h>
int main(int argc, char* argv[])
{
QByteArray field = "A8BC301F0193E83F";
bool ok;
qlonglong val = field.toLongLong(&ok, 16);
qDebug() << ok << val;
qulonglongval_u = field.toULongLong(&ok, 16);
qDebug() << ok << val_u << (long long)val_u;
long long check = 0xA8BC301F0193E83F;
qDebug() << check;
return 0;
}
EDIT: you can take a look at the source code of the associated helper function and see that this is indeed the case between signed and unsigned integers.
Upvotes: 2