Important: Please read the Qt Code of Conduct -

Issue in converting the string to integer!

  • Hi,

    I am unable to convert the string characters to integer in the following QString:
    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:

    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 =;
    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.

  • Lifetime Qt Champion

    \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?

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

  • @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) {
        qDebug() << numbers;
        return a.exec();

    with this output:

    QVector(0, 1, 2, 3, 4, 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)

    output: 0, 15, 2

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

    for(QString n : l)


    ("\u0001", "\u000F", "\u0002")

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

  • 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")

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


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

  • @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().

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

    I am unable to convert the string characters to integer in the following QString:
    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.

Log in to reply