# QString::toShort problem

• @kshegunov I still don't get it.
What is the representation of -2 as signed short? Isn't it 0xFFFE?

``````  0000 0000 0000 0010 - 2
1111 1111 1111 1101 - invert
+ 0000 0000 0000 0001 - add 1
1111 1111 1111 1110
-> 0xFFFE
``````

• OK, here's an exercise to settle the debate. First, assume that QString::toShort() behaves exactly as you expect.

What should each QString (p_*) be initialized to, in order to get `32` for every output line?

``````QString p_oct, p_dec, p_hex, p_r32;

// ... Initialize QStrings here ...

qDebug() << p_dec.toShort(nullptr, 10); // Returns 32
qDebug() << p_hex.toShort(nullptr, 16); // Returns 32

qDebug() << p_oct.toShort(nullptr, 8);  // Returns 32
qDebug() << p_r32.toShort(nullptr, 32); // Returns 32
``````

Next, what should each QString (n_*) be initialized to, in order to get `-32` for every output line?

``````QString n_oct, n_dec, n_hex, n_r32;

// ... Initialize QStrings here ...

qDebug() << n_oct.toShort(nullptr, 8);  // Returns -32
qDebug() << n_dec.toShort(nullptr, 10); // Returns -32
qDebug() << n_hex.toShort(nullptr, 16); // Returns -32
qDebug() << n_r32.toShort(nullptr, 32); // Returns -32
``````

• I don't get what you don't get about: `0xFFFE` is a positive overflow for parsing & storing into a `ushort`. Hence the behaviour.

One thing that is clear: the implementation of `QString::toShort()` is not `static_cast<short>(QString::toUShort())`, even if that might have been the way you were tempted to do it.

Nobody has looked at it "the other way round". I cannot test because I am Python/PyQt not C++, but what does

``````QString("-2").toUShort(&ok, 16)
``````

return? In your theory it should be `0xFFFE`, but I am "hoping"(!) it returns an error, just like `QString("FFFE").toShort(&ok, 16)` does?

Assuming that is the case, this means we do not have an ambiguity/duplication, whereby both `FFFE` and `-2` strings can be parsed as the same number by `toShort()`/`toUShort()` (but `2` is the only way to write +2).

• toShort makes a toLongLong interpretation first and than casts it to short theres where the "error" comes from:

``````short QString::toShort(bool *ok, int base) const
{
long v = toLongLong(ok, base);
if (v < SHRT_MIN || v > SHRT_MAX) {
if (ok)
*ok = false;
v = 0;
}
return (short)v;
}
``````

toLongLong will return ‭65534‬, (0xFFFE in int64 is positve after all), and that is bigger than SHRT_MAX -> 0 and failed conversion

• @JonB said in QString::toShort problem:

In your theory it should be 0xFFFE

No, it would not, because -2 is not a hex number...
"I don't get what you don't get about: 0xFFFE is a positive overflow for parsing & storing into a ushort" - we are not talking about unsigned short, but signed short and 0xFFFE is the representation of -2.

• @J.Hilk
In that case, try passing something like `0xFFFFFFFE` or `0xFFFFFFFFFFFFFFFE` for the string to `toShort()` and those who want `-2` instead of error should get it?!

• @jsulm

No, it would not, because -2 is not a hex number...

Yes it is! It's as much a hex number as some other base.

• @JonB said in QString::toShort problem:

In that case, try passing something like 0xFFFFFFFE or 0xFFFFFFFFFFFFFFFE for the string to toShort()

Come on - these numbers are NOT short. We should stay on topic.

• @jsulm said in QString::toShort problem:

Come on - these numbers are NOT short. We should stay on topic.

I beg your pardon!? I am totally on topic. I was replying to @J-Hilk 's display of the code of `QString::toShort()`. Did you try what I suggested rather than dismissing it as OT? In view of the code shown, I am trying to suggest what `0xFFF....` string `toShort()` will accept as representing a negative number....

• Nobody wants to try my exercises... (sad face)

• @JonB Passing 0xFFFFFFFE returns 0

• @JonB
actually, no take a look at toLongLong

``````qint64 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)

• @jsulm

@JonB Passing 0xFFFFFFFE returns 0

Since `QString::toLongLong()` returns a `qint64` (8 bytes, not 4), did you try `0xFFFFFFFFFFFFFFFE` ?

• @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 number `0xFFFFFFFE != 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)?

• @JonB

``````qDebug() << (short)0xFFFE;
``````

prints -2 as expected

• @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). And `QString::toShort()` is taking the latter interpretation, and hence erroring.

• @jsulm said in QString::toShort problem:

@JonB

``````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:

@JonB

``````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 not `static_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 does

``````qDebug() << (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 why `qDebug() << (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's `QString::toShort()` does or purports to do.

Basically, "cast-to-short" (`(short)`) has no concept ever of "overflow/error", but `QString::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.

• @jsulm

@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, for `QString::toUShort()` it's valid (65,534, which is OK for `ushort`), but for `QString::toShort()` it's a positive number greater than the positive limit of 32,767 for a `short` ("overflow").

• 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 set `ok=false` instead of returning `-2` ?

• @JonB
thatsa a rapidhole down QString and QLocal ... 😨😨

Still to early in the morning to explore that ;-)

• @JonB said in QString::toShort problem:

I thought you might be Russian: are you "Mafiosa", could you send some "heavies" round to @jsulm for me...?

No, I'm not russian, but let's assume I know a guy who knows a guy, who knows a guy ... ;)

What is it in the implementation of qint64 QString::toLongLong() which makes this set ok=false instead of returning -2 ?

https://code.woboq.org/qt5/qtbase/src/corelib/tools/qlocale.cpp.html#_ZNK11QLocaleData15numberToCLocaleE11QStringView6QFlagsIN7QLocale12NumberOptionEEP15QVarLengthArrayIcLi256EE

• @kshegunov
The link you provide, for `QLocaleData::numberToCLocale()`, is for outputting internal numbers -> external strings. For `QString::toLongLong()` I am looking for function code which is for inputting external string -> internal number, and see why `0xFFFFFFFFFFFFFFFE` is causing a conversion error?

• @JonB
no, @kshegunov is right,

toLongLong calls, stringtolonglong

``````qlonglong QLocaleData::stringToLongLong(QStringView str, int base, bool *ok,
QLocale::NumberOptions number_options) const
{
CharBuff buff;
if (!numberToCLocale(str, number_options, &buff)) {
if (ok != 0)
*ok = false;
return 0;
}
return bytearrayToLongLong(buff.constData(), base, ok);
}

``````

that calls numberToCLocale

and uses the resulting char array to call bytearrayToLongLong

``````
qlonglong QLocaleData::bytearrayToLongLong(const char *num, int base, bool *ok)
{
bool _ok;
const char *endptr;
if (*num == '\0') {
if (ok != 0)
*ok = false;
return 0;
}
qlonglong l = qstrtoll(num, &endptr, base, &_ok);
if (!_ok) {
if (ok != 0)
*ok = false;
return 0;
}
if (*endptr != '\0') {
// we stopped at a non-digit character after converting some digits
if (ok != 0)
*ok = false;
return 0;
}
if (ok != 0)
*ok = true;
return l;
}
``````
``````long long
qstrtoll(const char * nptr, const char **endptr, int base, bool *ok)
{
*ok = true;
errno = 0;
char *endptr2 = 0;
long long result = qt_strtoll(nptr, &endptr2, base);
if (endptr)
*endptr = endptr2;
if ((result == 0 || result == std::numeric_limits<long long>::min()
|| result == std::numeric_limits<long long>::max())
&& (errno || nptr == endptr2)) {
*ok = false;
return 0;
}
return result;
}
``````

like I said, a rRabbit Hole

• @J.Hilk
OK, in that case, what's the implementation of `qt_strtoll()`, when performed on `0xFFFFFFFFFFFFFFFE`? I'm expecting it to return `-2`, but I'm guessing it returns `std::numeric_limits<long long>::min()` (or maybe `::max()`), but why?

• @JonB said in QString::toShort problem:

@J.Hilk
OK, in that case, what's the implementation of `qt_strtoll()`,

I live to serve :)

``````/*-
*
* Copyright (c) 2011 The FreeBSD Foundation
* Portions of this software were developed by David Chisnall
* under sponsorship from the FreeBSD Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
*    may be used to endorse or promote products derived from this software
*    without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Convert a string to a long long integer.
*
* Assumes that the upper and lower case
* alphabets and digits are each contiguous.
*/
long long
qt_strtoll(const char * nptr, char **endptr, int base)
{
const char *s;
unsigned long long acc;
char c;
unsigned long long cutoff;
int neg, any, cutlim;
/*
* Skip white space and pick up leading +/- sign if any.
* If base is 0, allow 0x for hex and 0 for octal, else
* assume decimal; if base is already 16, allow 0x.
*/
s = nptr;
do {
c = *s++;
} while (ascii_isspace(c));
if (c == '-') {
neg = 1;
c = *s++;
} else {
neg = 0;
if (c == '+')
c = *s++;
}
if ((base == 0 || base == 16) &&
c == '0' && (*s == 'x' || *s == 'X') &&
((s[1] >= '0' && s[1] <= '9') ||
(s[1] >= 'A' && s[1] <= 'F') ||
(s[1] >= 'a' && s[1] <= 'f'))) {
c = s[1];
s += 2;
base = 16;
}
if (base == 0)
base = c == '0' ? 8 : 10;
acc = any = 0;
if (base < 2 || base > 36)
goto noconv;
/*
* Compute the cutoff value between legal numbers and illegal
* numbers.  That is the largest legal value, divided by the
* base.  An input number that is greater than this value, if
* followed by a legal input character, is too big.  One that
* is equal to this value may be valid or not; the limit
* between valid and invalid numbers is then based on the last
* digit.  For instance, if the range for quads is
* [-9223372036854775808..9223372036854775807] and the input base
* is 10, cutoff will be set to 922337203685477580 and cutlim to
* either 7 (neg==0) or 8 (neg==1), meaning that if we have
* accumulated a value > 922337203685477580, or equal but the
* next digit is > 7 (or 8), the number is too big, and we will
* return a range error.
*
* Set 'any' if any `digits' consumed; make it negative to indicate
* overflow.
*/
cutoff = neg ? (unsigned long long)-(LLONG_MIN + LLONG_MAX) + LLONG_MAX
: LLONG_MAX;
cutlim = cutoff % base;
cutoff /= base;
for ( ; ; c = *s++) {
if (c >= '0' && c <= '9')
c -= '0';
else if (c >= 'A' && c <= 'Z')
c -= 'A' - 10;
else if (c >= 'a' && c <= 'z')
c -= 'a' - 10;
else
break;
if (c >= base)
break;
if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
any = -1;
else {
any = 1;
acc *= base;
acc += c;
}
}
if (any < 0) {
acc = neg ? LLONG_MIN : LLONG_MAX;
errno = ERANGE;
} else if (!any) {
noconv:
errno = EINVAL;
} else if (neg)
acc = (unsigned long long) -(long long)acc;
if (endptr != NULL)
*endptr = const_cast<char *>(any ? s - 1 : nptr);
return (acc);
}
``````

that said, here, my goto online reference, for future selfresearch:
https://code.woboq.org/qt5/
this webside has the benefit of mouse navigation (right click) to functions and their declartions/defininations

• @J.Hilk
Thanks! So --- not that I claim to understand the code --- the ultimate reason `0xFFFFFFFFFFFFFFFE` errors as a `qint64` instead of returning `-2` must be the long comment about "Compute the cutoff value between legal numbers and illegal numbers.", the computation of `cutoff` & `cutlim` , and the test fragment `acc > cutoff || (acc == cutoff && c > cutlim)`.

• @JonB said in QString::toShort problem:

@J.Hilk
Thanks! So --- not that I claim to understand the code --- the ultimate reason `0xFFFFFFFFFFFFFFFE` errors as a `qint64` instead of returning `-2` must be the long comment about "Compute the cutoff value between legal numbers and illegal numbers.", the computation of `cutoff` & `cutlim` , and the test fragment `acc > cutoff || (acc == cutoff && c > cutlim)`.

Actually, I would consider this a bug, the "cutoff seems to happen one char to early

``````    qDebug() << 0xFFFFFFFFFFFFFFFF
<< static_cast<int64_t>(0xFFFFFFFFFFFFFFFF)
<< QString("0xFFFFFFFFFFFFFFFF").toLongLong(nullptr,16)
<< QString("0xFFFFFFFFFFFFFFF").toLongLong(nullptr,16)
<< QString::number(1152921504606846975,16)
<< QString::number(18446744073709551615,16)
<< QString::number(18446744073709551615,16).toLongLong(nullptr, 16);
//results in
18446744073709551615
-1
0
1152921504606846975
"fffffffffffffff"
"ffffffffffffffff"
0
``````

that makes `QString::number(18446744073709551615,16)` irreversible as `QString::number(18446744073709551615,16).toLongLong(&ok, 16);` result in false & 0(as value)

• @J.Hilk
I wish you'd write those long decimal numbers in hex so they're easier to understand!

I find this worrying, as I'm often needing to use 18446744073709551615 in everyday life (e.g. my prediction for number of goals England will score in World Cup).

As for your finding, are you suggesting the condition should have had `c >= cutlim` in it?