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. Issue in converting the string to integer!
Forum Updated to NodeBB v4.3 + New Features

Issue in converting the string to integer!

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 8 Posters 5.9k Views 5 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.
  • Q Offline
    Q Offline
    QtVik
    wrote on last edited by
    #1

    Hi,

    I am unable to convert the string characters to integer in the following QString:
    Example:
    QString myString("\000#\001#\002#\003#\004#\005");

    Now i want to extract only the numbers . How to achieve that ?

    Note: I have tried QString::split() as below but i am not able to extract the actual numbers:
    QStringList numStr = myString.split("#");

    Above split give list of strings as below:
    \001
    \002
    \002
    \004
    \005

    I have tried replacing "\00" but could not achieve the result.

    After that iterate through the loop to convert the above splitted string to integer like below:
    for(int i = 0 ; i < numStr.count(); i++)
    {
    QString tempstrIndex = numStr.at(i);
    int tempIndex = tempstrIndex.toInt();
    numIndex.push_back(tempIndex); //integer vector
    }
    from the above loop numIndex contains only zero '0'.

    Please advice me the right way to convert string to integer from above example.

    K 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      \0 is string terminator so
      its a very bad thing to have in strings.
      Would it be possible not to use \0 in the strings?

      1 Reply Last reply
      0
      • Q QtVik

        Hi,

        I am unable to convert the string characters to integer in the following QString:
        Example:
        QString myString("\000#\001#\002#\003#\004#\005");

        Now i want to extract only the numbers . How to achieve that ?

        Note: I have tried QString::split() as below but i am not able to extract the actual numbers:
        QStringList numStr = myString.split("#");

        Above split give list of strings as below:
        \001
        \002
        \002
        \004
        \005

        I have tried replacing "\00" but could not achieve the result.

        After that iterate through the loop to convert the above splitted string to integer like below:
        for(int i = 0 ; i < numStr.count(); i++)
        {
        QString tempstrIndex = numStr.at(i);
        int tempIndex = tempstrIndex.toInt();
        numIndex.push_back(tempIndex); //integer vector
        }
        from the above loop numIndex contains only zero '0'.

        Please advice me the right way to convert string to integer from above example.

        K Offline
        K Offline
        kenchan
        wrote on last edited by kenchan
        #3

        @QtVik
        what you have there is called a null terminated string list but there is usually a double terminator at the end of those.
        Anyways, you are probably better off using a QByteArray instead of a QString to handle that.
        The QByteArray can handle the \0 null chars and it has a split function and a number function :-).
        You can always put the split results back into a QString if you need.

        1 Reply Last reply
        1
        • Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on last edited by
          #4

          @QtVik as mentioned before, most of your issues come from using \0 within an string.
          You may need to escape such combination by using \ for the backslash. Here's a code snippet working that way:

          #include <QCoreApplication>
          #include <QStringList>
          #include <QVector>
          #include <QDebug>
          
          int main(int argc, char *argv[])
          {
              QCoreApplication a(argc, argv);
          
              QString myString("\\000#\\001#\\002#\\003#\\004#\\005");
              myString = myString.replace("\\", "");
              QStringList list = myString.split("#");
              QVector<int> numbers;
              for (QString number : list) {
                  numbers.append(number.toInt());
              }
              qDebug() << numbers;
          
              return a.exec();
          }
          

          with this output:

          QVector(0, 1, 2, 3, 4, 5)
          

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          Q 1 Reply Last reply
          0
          • M Offline
            M Offline
            mpergand
            wrote on last edited by mpergand
            #5

            If you want to deal with octal values in a string, simply do the following:

            QString myString("000#017#002");
            QStringList l=myString.split("#");
            
            for(QString n : l)
                qDebug()<<n.toInt(nullptr,8);
            

            output: 0, 15, 2

            If you're using escape, you'll get 3 QChar:

            myString="\001#\017#\002";
            l=myString.split("#");
            qDebug()<<l;
            for(QString n : l)
                qDebug()<<n[0].unicode();
            

            output:

            ("\u0001", "\u000F", "\u0002")
            1
            15
            2

            Anyway, the \000 null value issue still remains ...

            1 Reply Last reply
            0
            • Paul ColbyP Offline
              Paul ColbyP Offline
              Paul Colby
              wrote on last edited by
              #6

              Just something more to consider...

              There's absolutely nothing wrong with having null characters (\0) in C/C++ literals - it just means that you (or in this case QString) need to now the literal's string length. Many functions auto-detect the length by assuming that null characters are string terminators, but they don't have to be.

              For example, you can use your literal directly like:

                  const QString myString = QString::fromLocal8Bit("\000#\001#\002#\003#\004#\005", 11);
                  qDebug() << myString; 
                  qDebug() << myString.split("#");
              

              Of course this has the downside of requiring you, the developer, to update the length (11 above) if you ever change the literal. But this way there's no runtime character conversion to integer going one (or more importantly, if you want arbitrary Unicode chars, you can have them). Whether its worth it or not depends on your use case.

              The outputs of the above three lines is:

              "\u0000#\u0001#\u0002#\u0003#\u0004#\u0005"
              ("\u0000", "\u0001", "\u0002", "\u0003", "\u0004", "\u0005")
              

              ie that last row is a QStringList of 6 strings, each containing a single (possible invalid Unicode) character.

              Cheers.

              1 Reply Last reply
              0
              • Pablo J. RoginaP Pablo J. Rogina

                @QtVik as mentioned before, most of your issues come from using \0 within an string.
                You may need to escape such combination by using \ for the backslash. Here's a code snippet working that way:

                #include <QCoreApplication>
                #include <QStringList>
                #include <QVector>
                #include <QDebug>
                
                int main(int argc, char *argv[])
                {
                    QCoreApplication a(argc, argv);
                
                    QString myString("\\000#\\001#\\002#\\003#\\004#\\005");
                    myString = myString.replace("\\", "");
                    QStringList list = myString.split("#");
                    QVector<int> numbers;
                    for (QString number : list) {
                        numbers.append(number.toInt());
                    }
                    qDebug() << numbers;
                
                    return a.exec();
                }
                

                with this output:

                QVector(0, 1, 2, 3, 4, 5)
                
                Q Offline
                Q Offline
                QtVik
                wrote on last edited by QtVik
                #7

                @Pablo-J.-Rogina Thank you. The code doesn't work for the below string
                QString myString("\000#\001#\002#\003#\004#\005");

                JonBJ 1 Reply Last reply
                0
                • Q QtVik

                  @Pablo-J.-Rogina Thank you. The code doesn't work for the below string
                  QString myString("\000#\001#\002#\003#\004#\005");

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by JonB
                  #8

                  @QtVik
                  No, it won't because that's not the code he wrote.

                  In @Pablo-J-Rogina's method, you must include literal backslashes (\\) in the literal string, and then deal with them, as per his code.

                  In @Paul-Colby's method, you must pass the actual length of the literal string as a second parameter to QString::::fromLocal8Bit().

                  1 Reply Last reply
                  1
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #9

                    @QtVik said in Issue in converting the string to integer!:

                    I am unable to convert the string characters to integer in the following QString:
                    Example:
                    QString myString("\000#\001#\002#\003#\004#\005");

                    I'm guessing that this line don't exist in your code. If this is true, post the code that you are using.

                    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