Convert a QString to an uint8_t
-
wrote on 25 Nov 2021, 13:39 last edited by
Hello,
here is my problem :
I have a Qstring that contains "1.2" for example, what i want to do, is having two uint8_t , one with the number 1 inside and the other one with the number 2 inside. (Note that i would want the number, not the Ascii carac).
How i do :
QPair<uint8_t, uint8_t> _Version = 1.2; QStringList versionsNumbers = _Version.split("."); _Version.first = static_cast<uint8_t>(versionsNumbers.at(0).toInt()); _Version.second = static_cast<uint8_t>(versionsNumbers.at(1).toInt());
From what i saw, i cant just make a ".toInt()" without a cast or i would get a warning. And there is no function "toUin8_t", i just find .toUShort.
Is this the right solution?
-
Hello,
here is my problem :
I have a Qstring that contains "1.2" for example, what i want to do, is having two uint8_t , one with the number 1 inside and the other one with the number 2 inside. (Note that i would want the number, not the Ascii carac).
How i do :
QPair<uint8_t, uint8_t> _Version = 1.2; QStringList versionsNumbers = _Version.split("."); _Version.first = static_cast<uint8_t>(versionsNumbers.at(0).toInt()); _Version.second = static_cast<uint8_t>(versionsNumbers.at(1).toInt());
From what i saw, i cant just make a ".toInt()" without a cast or i would get a warning. And there is no function "toUin8_t", i just find .toUShort.
Is this the right solution?
@Pantoufle said in Convert a QString to an uint8_t:
QPair<uint8_t, uint8_t> _Version = 1.2;
I don't get it: you are talking about QString, but here you use a QPair?
What you can do is simply split the string at '.':QStringList parts = str.split('.'); uint8_t = static_cast<uint8_t>(parts[0].toInt()); // same for the second number
And yes you have to cast.
-
wrote on 25 Nov 2021, 13:51 last edited by
Oh sorry i miss copy my code :p
Here the real one :
QString VersionString = "1.2"; QPair<uint8_t, uint8_t> _Version; QStringList versionsNumbers = VersionString.split("."); _Version.first = static_cast<uint8_t>(versionsNumbers.at(0).toInt()); _Version.second = static_cast<uint8_t>(versionsNumbers.at(1).toInt());
-
wrote on 25 Nov 2021, 13:54 last edited by VRonin
Probably not worth it but given it's
uint8_t
you can use:const QString _Version = "1.2"; const auto versionsNumbers = _Version.splitRef("."); std::pair<uint8_t, uint8_t> verNums = std::make_pair<uint8_t, uint8_t>(versionsNumbers.at(0).at(0).unicode()-'0',versionsNumbers.at(1).at(0).unicode()-'0');
-
wrote on 25 Nov 2021, 14:01 last edited by
That seems more complicated than my solution to be honest :p
-
wrote on 25 Nov 2021, 14:19 last edited by
@Pantoufle said in Convert a QString to an uint8_t:
That seems more complicated than my solution to be honest :p
Indeed, hence my "Probably not worth it". It's more efficient though
-
Oh sorry i miss copy my code :p
Here the real one :
QString VersionString = "1.2"; QPair<uint8_t, uint8_t> _Version; QStringList versionsNumbers = VersionString.split("."); _Version.first = static_cast<uint8_t>(versionsNumbers.at(0).toInt()); _Version.second = static_cast<uint8_t>(versionsNumbers.at(1).toInt());
@Pantoufle said in Convert a QString to an uint8_t:
versionsNumbers
if you're not using
versionsNumbers
elsewhere in your current scope, I would usesplitRef
also list/array size checking before accessing 0 and 1 would be good to have as well.
-
Ok good to know.
@J-Hilk
I don't use it elsewhere.
For array size check, i always though that when you use .at(i) instead of [i], there is an automatic check to avoid reading overflow.
Maybe I'm wrong.wrote on 25 Nov 2021, 15:52 last edited by@Pantoufle said in Convert a QString to an uint8_t:
when you use .at(i) instead of [i], there is an automatic check to avoid reading overflow.
No,
value(i)
does,at(i)
lets it up to you to check -
wrote on 25 Nov 2021, 15:53 last edited by
Ok really good things to know thanks for your help guys
1/10