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 String to Decimal and and vice versa,
QtWS25 Last Chance

Convert String to Decimal and and vice versa,

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 573 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.
  • D Offline
    D Offline
    DQUY05
    wrote on 17 May 2023, 08:03 last edited by
    #1

    I want to convert QString sValue = "A"; to Decimal = 65, and vice versa Decimal = 81, then QString = "Q"

        QString sValue = "A";   
        bool bStatus = false;
        int nHex = sValue.toInt(&bStatus, 10);   
        ui->info_2->setText(QString::number(nHex));  // Always return = 0
    

    Can someone help me
    Thanks !

    J P 2 Replies Last reply 17 May 2023, 08:10
    0
    • D DQUY05
      17 May 2023, 08:03

      I want to convert QString sValue = "A"; to Decimal = 65, and vice versa Decimal = 81, then QString = "Q"

          QString sValue = "A";   
          bool bStatus = false;
          int nHex = sValue.toInt(&bStatus, 10);   
          ui->info_2->setText(QString::number(nHex));  // Always return = 0
      

      Can someone help me
      Thanks !

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 17 May 2023, 08:10 last edited by
      #2

      @DQUY05 said in Convert String to Decimal and and vice versa,:

      10

      16, not 10, your string is supposed to be hex interpreted, so 16


      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.

      D 1 Reply Last reply 17 May 2023, 08:16
      1
      • J J.Hilk
        17 May 2023, 08:10

        @DQUY05 said in Convert String to Decimal and and vice versa,:

        10

        16, not 10, your string is supposed to be hex interpreted, so 16

        D Offline
        D Offline
        DQUY05
        wrote on 17 May 2023, 08:16 last edited by
        #3

        @J-Hilk , thank Sir !
        I want to convert to Decimal not Hex, as you said that is not what I want

           QString sValue = "A";   
            bool bStatus = false;
            int out = sValue.toInt(&bStatus, 16);   
            ui->info_2->setText(QString::number(out));  // Always return = 10, I need it = 65, no need it = 10
        

        Thanks !

        J 1 Reply Last reply 17 May 2023, 08:26
        0
        • D DQUY05
          17 May 2023, 08:03

          I want to convert QString sValue = "A"; to Decimal = 65, and vice versa Decimal = 81, then QString = "Q"

              QString sValue = "A";   
              bool bStatus = false;
              int nHex = sValue.toInt(&bStatus, 10);   
              ui->info_2->setText(QString::number(nHex));  // Always return = 0
          

          Can someone help me
          Thanks !

          P Offline
          P Offline
          Pl45m4
          wrote on 17 May 2023, 08:17 last edited by
          #4

          @DQUY05

          So basically ASCII to Int conversion?!

          Have a look at

          • https://doc.qt.io/qt-6/qstring.html#toLatin1

          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          1
          • D DQUY05
            17 May 2023, 08:16

            @J-Hilk , thank Sir !
            I want to convert to Decimal not Hex, as you said that is not what I want

               QString sValue = "A";   
                bool bStatus = false;
                int out = sValue.toInt(&bStatus, 16);   
                ui->info_2->setText(QString::number(out));  // Always return = 10, I need it = 65, no need it = 10
            

            Thanks !

            J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 17 May 2023, 08:26 last edited by
            #5

            @DQUY05
            @Pl45m4 take a look at toLatin1() it should make your life easier, as long as you're sure your chars are ASCII chars.

            If not, take the later of your string (e.g.at(0)) as a QChar and call unicode() on it


            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.

            D 1 Reply Last reply 17 May 2023, 08:46
            1
            • J J.Hilk
              17 May 2023, 08:26

              @DQUY05
              @Pl45m4 take a look at toLatin1() it should make your life easier, as long as you're sure your chars are ASCII chars.

              If not, take the later of your string (e.g.at(0)) as a QChar and call unicode() on it

              D Offline
              D Offline
              DQUY05
              wrote on 17 May 2023, 08:46 last edited by
              #6

              @J-Hilk Dear Sir !
              As you said, I converted right

                  QString sValue = "A";
                  QChar out =  sValue.at(0);
                  ui->info_2->setText(QString::number(out.unicode(),10));  // return = 65 => OK
              
              

              I still don't know how to do the opposite, something like this

              int value=65;
              ui->info_2->setText(QString::fromUcs4(value));  // I need return = "A"
              

              Thanks !

              J 1 Reply Last reply 17 May 2023, 08:49
              0
              • D DQUY05
                17 May 2023, 08:46

                @J-Hilk Dear Sir !
                As you said, I converted right

                    QString sValue = "A";
                    QChar out =  sValue.at(0);
                    ui->info_2->setText(QString::number(out.unicode(),10));  // return = 65 => OK
                
                

                I still don't know how to do the opposite, something like this

                int value=65;
                ui->info_2->setText(QString::fromUcs4(value));  // I need return = "A"
                

                Thanks !

                J Offline
                J Offline
                J.Hilk
                Moderators
                wrote on 17 May 2023, 08:49 last edited by
                #7

                @DQUY05 said in Convert String to Decimal and and vice versa,:

                I still don't know how to do the opposite,

                int value=65;
                ui->info_2->setText(QString{QChar{value}}); 
                

                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.

                D 1 Reply Last reply 17 May 2023, 08:57
                2
                • J J.Hilk
                  17 May 2023, 08:49

                  @DQUY05 said in Convert String to Decimal and and vice versa,:

                  I still don't know how to do the opposite,

                  int value=65;
                  ui->info_2->setText(QString{QChar{value}}); 
                  
                  D Offline
                  D Offline
                  DQUY05
                  wrote on 17 May 2023, 08:57 last edited by
                  #8

                  @J-Hilk Yes, So great, Thank Sir !

                  1 Reply Last reply
                  2
                  • D DQUY05 has marked this topic as solved on 17 May 2023, 08:57

                  4/8

                  17 May 2023, 08:17

                  • Login

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