Convert a QString to an uint8_t
-
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 numberAnd yes you have to cast.
-
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()); -
Probably not worth it but given it's
uint8_tyou 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'); -
@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
versionsNumberselsewhere in your current scope, I would usesplitRefalso 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.@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