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
Forum Updated to NodeBB v4.3 + New Features

Convert a QString to an uint8_t

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 2.8k 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 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?

    jsulmJ 1 Reply Last reply
    0
    • P Pantoufle

      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?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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 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.HilkJ 1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on 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 last edited by
            #5

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

            VRoninV 1 Reply Last reply
            0
            • P Pantoufle

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

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on 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

                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.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on 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 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.

                  VRoninV 1 Reply Last reply
                  0
                  • P Pantoufle

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

                    VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on 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 last edited by
                      #10

                      Ok really good things to know thanks for your help guys

                      1 Reply Last reply
                      0

                      • Login

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