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. What is the prefix of a QString for QString::toInt(&ok, 2)?
Forum Updated to NodeBB v4.3 + New Features

What is the prefix of a QString for QString::toInt(&ok, 2)?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 4 Posters 615 Views 3 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.
  • I Offline
    I Offline
    Infinity
    wrote on last edited by Infinity
    #1

    I want to convert a QString to an int with QString::toInt() with the following functions:

    bool QStringToInt::setIntString(const QString intString)
    {
        m_intNumber = 0;
    
        // The input string is in decimal format
        if (this->stringToInt(intString, 10)) return true;
    
        // The input string is in hexadecimal format
        if (this->stringToInt(intString, 16)) return true;
    
        // The input string is in binary format
        if (this->stringToInt(intString, 2)) return true;
    
        qDebug() << "\n";
        qDebug() << "QStringToInt::setIntString;";
        qDebug().noquote() << "Invalid int string";
    
        return false;
    }
    
    bool QStringToInt::stringToInt(const QString intString, const int base)
    {
        bool intStringOk;
    
        uint intNumber = intString.toUInt(&intStringOk, base);
    
        if (intStringOk) {
            m_intNumber = intNumber;
            return true;
        } else {
            m_intNumber = 0;
            return false;
        }
    }
    

    Which prefix do I have to put in front of the intString that

    if (this->stringToInt(intString, 2))
    

    returns true and:

    stringToInt(intString, 10)
    
    stringToInt(intString, 16)
    

    return false?

    If I put "0x" in front of a string, QString::toInt(&ok, 10) set ok to false and QString::toInt(&ok, 16) sets ok to true. Which is good.

    Which prefix do I have to put in front of the string that QString::toInt(&ok, 10) set ok to false, QString::toInt(&ok, 16) sets ok to false and QString::toInt(&ok, 2) sets ok to true?

    aha_1980A 1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #3

      Those ifs in ::setIntString() are wrong. You can't detect the base like that. For example 0 could be binary, octal, decimal, anything from base 1 up really and all those ifs will return true. It just so happens that you treat it as a base 10 first.

      QString::toUint only checks that you don't use invalid digits for given base. It can't detect what base the string is, except for the 3 predefined prefixes, but that only works if you pass 0 as base. Otherwise 011 will still be treated as decimal if you pass 10 as base, though it was probably intended as octal.

      1 Reply Last reply
      4
      • I Infinity

        I want to convert a QString to an int with QString::toInt() with the following functions:

        bool QStringToInt::setIntString(const QString intString)
        {
            m_intNumber = 0;
        
            // The input string is in decimal format
            if (this->stringToInt(intString, 10)) return true;
        
            // The input string is in hexadecimal format
            if (this->stringToInt(intString, 16)) return true;
        
            // The input string is in binary format
            if (this->stringToInt(intString, 2)) return true;
        
            qDebug() << "\n";
            qDebug() << "QStringToInt::setIntString;";
            qDebug().noquote() << "Invalid int string";
        
            return false;
        }
        
        bool QStringToInt::stringToInt(const QString intString, const int base)
        {
            bool intStringOk;
        
            uint intNumber = intString.toUInt(&intStringOk, base);
        
            if (intStringOk) {
                m_intNumber = intNumber;
                return true;
            } else {
                m_intNumber = 0;
                return false;
            }
        }
        

        Which prefix do I have to put in front of the intString that

        if (this->stringToInt(intString, 2))
        

        returns true and:

        stringToInt(intString, 10)
        
        stringToInt(intString, 16)
        

        return false?

        If I put "0x" in front of a string, QString::toInt(&ok, 10) set ok to false and QString::toInt(&ok, 16) sets ok to true. Which is good.

        Which prefix do I have to put in front of the string that QString::toInt(&ok, 10) set ok to false, QString::toInt(&ok, 16) sets ok to false and QString::toInt(&ok, 2) sets ok to true?

        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi @Infinity,

        as the docu states, the is no binary prefix evaluation, probably because C never had it.

        But if you state base 2 explicitly, it should work if the string only contains 0 and 1.

        Regards

        Qt has to stay free or it will die.

        1 Reply Last reply
        3
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by Chris Kawa
          #3

          Those ifs in ::setIntString() are wrong. You can't detect the base like that. For example 0 could be binary, octal, decimal, anything from base 1 up really and all those ifs will return true. It just so happens that you treat it as a base 10 first.

          QString::toUint only checks that you don't use invalid digits for given base. It can't detect what base the string is, except for the 3 predefined prefixes, but that only works if you pass 0 as base. Otherwise 011 will still be treated as decimal if you pass 10 as base, though it was probably intended as octal.

          1 Reply Last reply
          4
          • Kent-DorfmanK Offline
            Kent-DorfmanK Offline
            Kent-Dorfman
            wrote on last edited by
            #4

            yeah, detecting the base of a number from its string representation is somewhat haphazard unless you are prepared to adopt certain rules and create a parser specifically for that function, such as rules that mimmick the C/C++ numeric literal rules.
            0x for hex
            0nnn for octal
            [1-9][0-9]* for decimal
            and i dunno for binary because I always use hex instead

            I light my way forward with the fires of all the bridges I've burned behind me.

            1 Reply Last reply
            1

            • Login

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