Unsolved QByteArray toLong false
-
I thought the same at first, but the "16" indicates that it should get interpreted as hex (base 16).
-
Well, I don't know what you're expecting... AFAIK
long
is at most 64 bits, therefore any hex string longer than"0x8000000000000000"
won't give you anything meaningful. Apparently python's way is different, but this number is way to large to be handled properly using C++ native integer types. -
Sure, the conversion should fail. But it should not crash.
-
@sierdzio said in QByteArray toLong crash:
Sure, the conversion should fail. But it should not crash.
and it doesn't, at least in my test case:
int main (int argc, char *argv[]) { QApplication app(argc, argv); QByteArray a= "00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7"; bool ok; long la = a.toLong(&ok, 16); qDebug() << a << ok; return la; }
-
Hm, interesting. So, @sonichy either you have discovered a crash on some specific compiler or platform, or the crash you see comes from some other part of your code.
-
@sierdzio said in QByteArray toLong crash:
or the crash you see comes from some other part of your code.
I'm pretty sure :)
Btw, does not crash on Linux with Qt 5.14.1
Regards
-
@sonichy said in QByteArray toLong crash:
or the crash you see
What crash? Please post the actual stack trace
-
@JohanSolo said in QByteArray toLong crash:
Apparently python's way is different
Seems like
Python
has an unlimited long int precision,Plain integers (also just called integers) are implemented using long in C, which gives them at least 32 bits of precision. Long integers have unlimited precision. Floating point numbers are implemented using double in C.
(from: https://docs.python.org/2.4/lib/typesnumeric.html)
while C++ doesn't have it (of course). Usually 16b, 32b, 64b for
short
,int
,long
.@sonichy
So you can't process your large byte array as single value in C++.@J-Hilk
Haven't tested myself, but does yourlong
really equal the value of that byte array, or isqDebug()
just printing an array of hex chars?
Nevermind, I'm an idiot :D You do print thatQByteArray
, not the actuallong int
... :)Edit: Interesting, that you ( @J-Hilk )
return la
and the value is0
. The null could be the reason for the crash, @sonichy is facing. Depends on where and how thatlong
is used later in the code. -
@Pl45m4 said in QByteArray toLong crash:
Edit: Interesting, that you ( @J-Hilk ) return la and the value is 0. The null could be the reason for the crash, @sonichy is facing. Depends on where and how that long is used later in the code.
? Even if the value is 0 (I don't know whether it is or not), it's a number, zero, not a pointer, null, and won't (certinaly shouldn't) be used as such later. Why do you feel that would influence a crash?
-
@JonB said in QByteArray toLong crash:
Why do you feel that would influence a crash?
val = ba.toLong(...); // returns 0
newVal = 42 / val:)
-
@Christian-Ehrlicher
Don't be silly ;-) The input stream is presumably some numbers, the fact that one comes out as 0 shouldn't lead it to be treated as a null pointer is all I'm saying :) -
As I've said, I dont know what OP is trying to do with his
long
later ( sounds wrong... :-D ).
If it's somehow important for the value to be higher than 0, this would definitely crash then... Just my thoughts.
And I didnt mentioned any pointer :)