How to write ULONG into QVariant ?
-
I have a 'ulong' with the value '18446744073709551615', this is the absolute maximum value.
I can see in the debugger that a QVariant has the correct value stored:
1.8446744073709552e+19
I try to copy this from the variable into a type of ulong:
ulong ulngCheck; memcpy(&ulngCheck, varCheck.data(), sizeof(ulngCheck)); blnPass = ((ulngCheck - ulngData) == 0);
ulngData is another ulong which does have the value:
18446744073709551615
When I single step in the debugger over the memcpy operation, ulngCheck has the value:
4895412794951729152
Why ?
-
Hi,
Using:
ulong ulngCheck = varCheck.value<ulong>();
would likely be simpler.
[edit: fixed type SGaist]
-
@SGaist said in How to write ULONG into QVariant ?:
long
Thanks, slight alteration:
ulong ulngCheck = varCheck.value<ulong>();
There are a few types where this method fails, these are: char, uchar and ulong, in the case of char and uchar using:
char uValue = varCheck.value<char>(); or uchar ucValue = varCheck.value<uchar>();
The returned value is 0, yet in the debugger varCheck contains 'a'.
ulong ulngValue = varCheck.value<ulong>();
varCheck contains: 1.8446744073709552e+19
Yet, ulngValue is converted to: 9223372036854775808
So the result still isn't correct.The only way I found of getting the correct value back out of varCheck when it is assigned a ulong is:
double dblCheck = varCheck.value<double>();
This then extracts: 1.8446744073709552e+19.
-
Provide some code instead pretty printed output from the debugger:
int main(int argc, char *argv[]) { QApplication a(argc, argv); qulonglong ql = 18446744073709551615ULL; uint64_t ui = ql; unsigned long ul1 = ql; ulong ul2 = ql; qDebug() << sizeof(ql) << sizeof(ui) << sizeof(ul1) << sizeof(ul2); qDebug() << ql << ui << ul1 << ul2; QVariant v1(ql); ulong out = v1.value<ulong>(); qDebug() << out; }
-->
8 8 8 8
18446744073709551615 18446744073709551615 18446744073709551615 18446744073709551615
18446744073709551615 -
@Christian-Ehrlicher, does your example produce the same result with:
ulong ul2 = 18446744073709551615; QVariant v1(ul2);
?
-
@SPlatten said in How to write ULONG into QVariant ?:
does your example produce the same result with:
So hard to try it by yourself? Your compiler should inform you about what you're doing wrong btw...
/edit: http://www.cplusplus.com/doc/tutorial/constants/
"These literal constants have a type, just like variables. By default, integer literals are of type int. "