Unsolved QString::toShort problem
-
@JonB
actually, no take a look at toLongLongqint64 QString::toLongLong(bool *ok, int base) const { #if defined(QT_CHECK_RANGE) if (base != 0 && (base < 2 || base > 36)) { qWarning("QString::toLongLong: Invalid base (%d)", base); base = 10; } #endif bool my_ok; QLocale def_locale; qint64 result = def_locale.d()->stringToLongLong(*this, base, &my_ok, QLocalePrivate::FailOnGroupSeparators); if (my_ok) { if (ok != 0) *ok = true; return result; } QLocale c_locale(QLocale::C); return c_locale.d()->stringToLongLong(*this, base, ok, QLocalePrivate::FailOnGroupSeparators); }
I think, haven't looked stringToLongLong up, that here happens stirng lentgh magic, because every combinaion of FFF..E up to to 0xFFFFFFFE is interpretated as the uint value and everything above as -2 (as returning int64 value)
-
-
@JonB said in QString::toShort problem:
Since QString::toLongLong() returns a qint64 (8 bytes), did you try 0xFFFFFFFFFFFFFFFE ?
Returns 0 as well.
And I don't see why it should depend on the length. -
@JonB surprisingly enough
qDebug() << std::numeric_limits<int64_t>::min() << std::numeric_limits<int64_t>::max() << endl << (int64_t)0xFFFFFFFFFFFFFFFE; QString s("0xFFFFFFFFFFFFFFFE"); bool ok; short sh = s.toShort(&ok, 16); qDebug() <<sh << ok; long lg = s.toLongLong(&ok,16); qDebug() << lg << ok;
returns:
-9223372036854775808 9223372036854775807 -2 0 false 0 false
-
@jsulm
It would "depend on the length", as you put it, because as a 64-bit number0xFFFFFFFE != 0xFFFFFFFFFFFFFFFE
. -
@JonB I want to convert a signed short number not long or long long or ...
0xFFFE as signed short is -2 - do you agree (I mean independently from what Qt toShort() thinks it is)? -
-
@jsulm
I believe the problem here is a confusion between "bit representation" and "string representation".- It is undoubtedly, unambiguously true that, for signed short,
0xFFFE
as a bit pattern is -2. - However, for signed short,
0xFFFE
as a string "could" be either -2 (which fits in a short) or 65,534 (which does not fit in a short). AndQString::toShort()
is taking the latter interpretation, and hence erroring.
- It is undoubtedly, unambiguously true that, for signed short,
-
@jsulm said in QString::toShort problem:
qDebug() << (short)0xFFFE;
prints -2 as expected
qDebug() << (short)0xFFFFFFFFFFFFFFFE;
prints also -2, would one expect that
actually yes, the first bytes are simply dropped x)
-
@jsulm said in QString::toShort problem:
qDebug() << (short)0xFFFE;
prints -2 as expected
Yes, that's why I wrote earlier:
One thing that is clear: the implementation of
QString::toShort()
is notstatic_cast<short>(QString::toUShort())
, even if that might have been the way you were tempted to do it. -
@JonB said in QString::toShort problem:
However, for signed short, 0xFFFE as a string "could" be either -2 (which fits in a short) or 65,534
No, signed short 0xFFFE is -2 even as string, because I'm calling toShort() not toUShort().
And why doesqDebug() << (short)0xFFFE;
print -2?
-
@JonB said in QString::toShort problem:
the implementation of QString::toShort() is not static_cast<short>(QString::toUShort())
I never said that
-
@jsulm
But you're asking whyqDebug() << (short)0xFFFE;
prints-2
. And I'm saying that's because of the way "cast-to-short" works in C++, which is simply not what the implementation of Qt'sQString::toShort()
does or purports to do.Basically, "cast-to-short" (
(short)
) has no concept ever of "overflow/error", butQString::toShort()
does have a concept of "overflow/error", and that's why they work differently. They are not intended to be equivalent.[I am beginning to feel the need for @kshegunov 's moral support here, because I feel I am being attacked ( :( ) and it is indeed all to do with the overflowing he mentioned in his earlier reply.]
-
@JonB What overflow error do you mean? 0xFFFE is a valid short number in both cases: signed and unsigned.
-
Hey, let's do it the russian way and settle this outside, huh? Take a breath people.
@jsulm
Johann, you're wrong simply because"0xFFFE"
is not a negative number, but a string, that simple. I know that in 2's complement for short this is -2, but that's if you go to the actual implementation of the negative numbers. The fact of the matter is there have been implementations that do not use integer complements. This string is not a binary representation, that is all, so don't expect the function to assume it should convert in binary-like way! Otherwise, as Jonas pointed out earlier"0xFFFFFFFFFFE"
should just expand to -2 as well due to truncations. -
@JonB What overflow error do you mean? 0xFFFE is a valid short number in both cases: signed and unsigned.
0xFFFE
as a bit-pattern is indeed a valid signed or unsigned bit-pattern for a short. But as a string to parse, forQString::toUShort()
it's valid (65,534, which is OK forushort
), but forQString::toShort()
it's a positive number greater than the positive limit of 32,767 for ashort
("overflow"). -
@kshegunov said in QString::toShort problem:
Hey, let's do it the russian way and settle this outside, huh? Take a breath people.
LOL! Phew, that's what I needed from you! I thought you might be Russian: are you "Mafiosa", could you send some "heavies" round to @jsulm for me...? ;-)
-
@kshegunov "Hey, let's do it the russian way and settle this outside, huh?" - wait a bit I need to collect some more guys to have better arguments :-)
OK, I see. But actually Qt "knows" for which platform it was built (2's complement or something else) and could interpret such strings accordingly. I guess Qt devs wanted to go safe route :-)
@Bart_Vandewoestyne I would say @kshegunov suggested the correct solution:short hex2 = static_cast<short>(str2.toUShort(&ok2, 16));
-
@J.Hilk said in QString::toShort problem:
QString s("0xFFFFFFFFFFFFFFFE"); bool ok;
long lg = s.toLongLong(&ok,16);
What is it in the implementation of
qint64 QString::toLongLong()
which makes this setok=false
instead of returning-2
? -
@JonB
thatsa a rapidhole down QString and QLocal ... 😨😨Still to early in the morning to explore that ;-)