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

Convert String to Decimal and and vice versa,

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 593 Views 1 Watching
  • 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.
  • DQUY05D Offline
    DQUY05D Offline
    DQUY05
    wrote on 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.HilkJ Pl45m4P 2 Replies Last reply
    0
    • DQUY05D DQUY05

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

      DQUY05D 1 Reply Last reply
      1
      • J.HilkJ J.Hilk

        @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

        DQUY05D Offline
        DQUY05D Offline
        DQUY05
        wrote on 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.HilkJ 1 Reply Last reply
        0
        • DQUY05D DQUY05

          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 !

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on 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
          • DQUY05D DQUY05

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

            DQUY05D 1 Reply Last reply
            1
            • J.HilkJ J.Hilk

              @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

              DQUY05D Offline
              DQUY05D Offline
              DQUY05
              wrote on 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.HilkJ 1 Reply Last reply
              0
              • DQUY05D DQUY05

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

                DQUY05D 1 Reply Last reply
                2
                • J.HilkJ J.Hilk

                  @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}}); 
                  
                  DQUY05D Offline
                  DQUY05D Offline
                  DQUY05
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  2
                  • DQUY05D DQUY05 has marked this topic as solved on

                  • Login

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