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. How can i convert a single character in a QString into an integer ?
Forum Updated to NodeBB v4.3 + New Features

How can i convert a single character in a QString into an integer ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 7.6k 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.
  • A Offline
    A Offline
    Ahti
    wrote on 7 Sept 2016, 17:03 last edited by
    #1

    Let me explain with the help of an example:-

    say i have this QString :

    int num = 0 ;
    QString test = "1A52D" ;
    

    and i want to convert a single character in a string ( say 2 ) into an integer so that the integer variable 'num' should store 2 in it.

    this is what i do:

    int num = 0 ;
    QString test = "1A52D" ;
    QString test123 = test[3] ;
    num = test123.toInt() ;
    

    when i run that code i get this error message :

    "conversion from QCharRef to non-scalar type QString required"

    what is a signature ?? Lol

    P 1 Reply Last reply 10 Oct 2016, 18:54
    0
    • C Offline
      C Offline
      Camilo del Real
      wrote on 7 Sept 2016, 17:16 last edited by
      #2

      QString test = "1A52D" ;
      int test123 = test[2].digitValue();

      1 Reply Last reply
      3
      • P Offline
        P Offline
        Pradeep Kumar
        wrote on 10 Oct 2016, 18:01 last edited by
        #3

        Hi,
        @Ahti

        The below link might be useful to you.

        https://forum.qt.io/topic/14739/converting-one-element-from-a-qstring-to-int-solved/6

        which converts a character of a string to int.

        Thanks,

        Pradeep Kumar
        Qt,QML Developer

        1 Reply Last reply
        1
        • A Ahti
          7 Sept 2016, 17:03

          Let me explain with the help of an example:-

          say i have this QString :

          int num = 0 ;
          QString test = "1A52D" ;
          

          and i want to convert a single character in a string ( say 2 ) into an integer so that the integer variable 'num' should store 2 in it.

          this is what i do:

          int num = 0 ;
          QString test = "1A52D" ;
          QString test123 = test[3] ;
          num = test123.toInt() ;
          

          when i run that code i get this error message :

          "conversion from QCharRef to non-scalar type QString required"

          P Offline
          P Offline
          Pradeep Kumar
          wrote on 10 Oct 2016, 18:54 last edited by
          #4

          With your example you have taken
          QString stringValue = "1A52D" ;

          Sample Code:

            QString stringValue = "1A52D" ;
             int num = 0 ;
            QStringList listStr;
          
          for (int var = 0; var < stringValue.length(); ++var) {
              if (stringValue.at(var).isDigit()){
                  int StringValue = stringValue.at(var).digitValue();
                  qDebug () << "firstDigit" << StringValue << endl;
                  listStr.append(QString::number(StringValue));
              }
          }
          

          Iterate and get the numeric values and add to StringList then get the values from StringList convert to int.

          QString oneValue  = listStr.value(0);
          QString twoValue  = listStr.value(0+1);
          QString threeValue  = listStr.value(0+2);
          
          qDebug() << " value" << oneValue << twoValue << threeValue <<  endl;
          
          qDebug() <<  oneValue.toInt();
          qDebug() <<  twoValue.toInt();
          qDebug() <<  threeValue.toInt();
          

          Assign which number you want to variable num.

          Pradeep Kumar
          Qt,QML Developer

          1 Reply Last reply
          2

          • Login

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