Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Convert a QString to an uint8_t
QtWS25 Last Chance

Convert a QString to an uint8_t

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 2.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pantoufle
    wrote on 25 Nov 2021, 13:39 last edited by
    #1

    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?

    J 1 Reply Last reply 25 Nov 2021, 13:47
    0
    • P Pantoufle
      25 Nov 2021, 13:39

      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?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 25 Nov 2021, 13:47 last edited by
      #2

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • P Offline
        P Offline
        Pantoufle
        wrote on 25 Nov 2021, 13:51 last edited by
        #3

        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());
        
        J 1 Reply Last reply 25 Nov 2021, 14:22
        0
        • V Offline
          V Offline
          VRonin
          wrote on 25 Nov 2021, 13:54 last edited by VRonin
          #4

          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');
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          0
          • P Offline
            P Offline
            Pantoufle
            wrote on 25 Nov 2021, 14:01 last edited by
            #5

            That seems more complicated than my solution to be honest :p

            V 1 Reply Last reply 25 Nov 2021, 14:19
            0
            • P Pantoufle
              25 Nov 2021, 14:01

              That seems more complicated than my solution to be honest :p

              V Offline
              V Offline
              VRonin
              wrote on 25 Nov 2021, 14:19 last edited by
              #6

              @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

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              0
              • P Pantoufle
                25 Nov 2021, 13:51

                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());
                
                J Online
                J Online
                J.Hilk
                Moderators
                wrote on 25 Nov 2021, 14:22 last edited by
                #7

                @Pantoufle said in Convert a QString to an uint8_t:

                versionsNumbers

                if you're not using versionsNumbers elsewhere in your current scope, I would use splitRef

                also list/array size checking before accessing 0 and 1 would be good to have as well.


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                1
                • P Offline
                  P Offline
                  Pantoufle
                  wrote on 25 Nov 2021, 15:42 last edited by
                  #8

                  @VRonin

                  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.

                  V 1 Reply Last reply 25 Nov 2021, 15:52
                  0
                  • P Pantoufle
                    25 Nov 2021, 15:42

                    @VRonin

                    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.

                    V Offline
                    V Offline
                    VRonin
                    wrote on 25 Nov 2021, 15:52 last edited by
                    #9

                    @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

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    1
                    • P Offline
                      P Offline
                      Pantoufle
                      wrote on 25 Nov 2021, 15:53 last edited by
                      #10

                      Ok really good things to know thanks for your help guys

                      1 Reply Last reply
                      0

                      1/10

                      25 Nov 2021, 13:39

                      • Login

                      • Login or register to search.
                      1 out of 10
                      • First post
                        1/10
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved