QString setnum behavior when inputted value is negative
-
What is the expected behavior when the inputted value of a QString is negative, and setnum is operated on it?
-
What is the expected behavior when the inputted value of a QString is negative, and setnum is operated on it?
@Dummie1138
str.setNum(-1234);
should presumably set the string to"-1234"
. -
@Dummie1138
str.setNum(-1234);
should presumably set the string to"-1234"
.@JonB What about when it is not base 10? For example, in base 16, 1234 is 4D2. Will setNum return "-4D2" or something else?
-
@JonB What about when it is not base 10? For example, in base 16, 1234 is 4D2. Will setNum return "-4D2" or something else?
@Dummie1138 Please read documentation: https://doc.qt.io/qt-6/qstring.html#setNum
QString &QString::setNum(int n, int base = 10)
-
@Dummie1138 Please read documentation: https://doc.qt.io/qt-6/qstring.html#setNum
QString &QString::setNum(int n, int base = 10)
I have visited this screen before asking this question. Sadly, like my previous visit, I was unable to find a reference to setnum behavior on a non-decimal, negative number. Please let me know if I missed something.
-
I have visited this screen before asking this question. Sadly, like my previous visit, I was unable to find a reference to setnum behavior on a non-decimal, negative number. Please let me know if I missed something.
@Dummie1138 why don't you test ist ?
setting up a simple main.cpp with setNum and qdebug takes less than a minute.
-
I have visited this screen before asking this question. Sadly, like my previous visit, I was unable to find a reference to setnum behavior on a non-decimal, negative number. Please let me know if I missed something.
@Dummie1138 said in QString setnum behavior when inputted value is negative:
I have visited this screen before asking this question. Sadly, like my previous visit, I was unable to find a reference to setnum behavior on a non-decimal, negative number. Please let me know if I missed something.
Not sure what you really want to do,
if you want to print a negative value in hex with a minus sign in front, you can try this:QString negHex(int v) { if(v>=0) return QString::number(v,16); // negative number v=~v+1; // two's complement return QString("-%1").arg(v,0,16); }
examples:
qDebug()<<negHex(1234)<<negHex(-1234)<<negHex(-0x4d2); ---> "4d2" "-4d2" "-4d2"
-
@Dummie1138 Negative numbers are supported in any base the same. If you pass -1234 in base 16 you will get "-4d2". As @J-Hilk said, it would've taken you less time to just try than post the question.
@mpergand That is absolutely unnecessary. QString's number conversion functions like
number
andsetNum
handle minus sign already. No need to reinvent the wheel. -
@Dummie1138 Negative numbers are supported in any base the same. If you pass -1234 in base 16 you will get "-4d2". As @J-Hilk said, it would've taken you less time to just try than post the question.
@mpergand That is absolutely unnecessary. QString's number conversion functions like
number
andsetNum
handle minus sign already. No need to reinvent the wheel.@Chris-Kawa said in QString setnum behavior when inputted value is negative:
If you pass -1234 in base 16 you will get "-4d2"
I don't get it ...
QString::number(-1234,16); -->"fffffffffffffb2e" -
@Chris-Kawa said in QString setnum behavior when inputted value is negative:
If you pass -1234 in base 16 you will get "-4d2"
I don't get it ...
QString::number(-1234,16); -->"fffffffffffffb2e"@mpergand Sorry about that. It seems there's more to it. The documentation for the
long
overload ofQString::number
says that for bases other than 10 the argument is treated as unsigned. What that means... is kinda ambiguous. The other overloads are not documented :/
It does return "-4d2" on my machine (msvc x64 Qt 6.4.2) for the int overload and looking at the source code it does handle the negative numbers by converting to unsigned and adding-
in al bases, so it looks like a bit of a mess. -
@mpergand Sorry about that. It seems there's more to it. The documentation for the
long
overload ofQString::number
says that for bases other than 10 the argument is treated as unsigned. What that means... is kinda ambiguous. The other overloads are not documented :/
It does return "-4d2" on my machine (msvc x64 Qt 6.4.2) for the int overload and looking at the source code it does handle the negative numbers by converting to unsigned and adding-
in al bases, so it looks like a bit of a mess.@Chris-Kawa
For me, negative hex values are represented like this:-1 --> FF (8bits) FFFF (16 bits) etc
It seems things have changed in newer versions of C++ as they allow float representation in hex (strange at first thought to me)
Maybe Qt6 has adopted this new capabilities
versus Qt5 ? -
@Chris-Kawa
For me, negative hex values are represented like this:-1 --> FF (8bits) FFFF (16 bits) etc
It seems things have changed in newer versions of C++ as they allow float representation in hex (strange at first thought to me)
Maybe Qt6 has adopted this new capabilities
versus Qt5 ?@mpergand Qt does not use standard library for number to string conversions, but the source here differs quite a lot from what I see locally in the source downloaded via online installer for Qt 6.4.2. So there seem to be some changes made, I just don't know when exactly, the easiest would probably be check the history in git, but I don't have the repo set up at hand.
What platform, toolset and Qt version are you using? -
@mpergand Qt does not use standard library for number to string conversions, but the source here differs quite a lot from what I see locally in the source downloaded via online installer for Qt 6.4.2. So there seem to be some changes made, I just don't know when exactly, the easiest would probably be check the history in git, but I don't have the repo set up at hand.
What platform, toolset and Qt version are you using?@Chris-Kawa said in QString setnum behavior when inputted value is negative:
What platform, toolset and Qt version are you using?
Qt 5.12.10, OSX 10.14 and Kubuntu 21.04