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. Error in QString.toUInt/Int/Double : Conversion fails
Forum Updated to NodeBB v4.3 + New Features

Error in QString.toUInt/Int/Double : Conversion fails

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 409 Views 2 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.
  • K Offline
    K Offline
    Ketank16
    wrote on 9 Oct 2019, 15:17 last edited by Ketank16 10 Sept 2019, 15:18
    #1

    Hello, I'm receiving data over serial port like this: CH0:0x20FC0C05and splitting it in form like this: sep: ("CH0:0", "20FC0C05")

    I can split further and copy the data into another string variable data: "20FC0C05". The problem comes when I'm trying to use data.toUInt function. It always returns 0 and I'm not able to figure it out.

    Code snippet:

    if(line != NULL)
                {
                    //qDebug() << "line: " << line;
                    QStringList sep = line.split('x');
                    QString data;
                    //qDebug() << "sep: " << sep;
                    if(sep.at(0).startsWith("CH0"))
                    {
                        qDebug() << "sep: " << sep;
                        data.append(sep.takeLast());
                        qDebug() << "data: " << data << data.toUInt(&ok, 10) << ok;
                        //qDebug() << "sep: " << sep.takeLast().toInt(&ok,10);
                        //ch0_array[ch0_index] = sep.takeLast().toDouble();
                        //ch0_index += 1;
                        //qDebug() << "ch0: " << ch0_array;
                    }
                }
    

    Output preview:

    sep:  ("CH0:0", "20FB3500")
    data:  "20FB3500" 0 false
    sep:  ("CH0:0", "20FA040D")
    data:  "20FA040D" 0 false
    sep:  ("CH0:0", "20F88105")
    data:  "20F88105" 0 false
    sep:  ("CH0:0", "20FF240A")
    data:  "20FF240A" 0 false
    sep:  ("CH0:0", "2105C003")
    

    I've also tried toInt and toDouble functions but the result looks the same. If anyone has idea whats going on here, that'll be great.

    P 1 Reply Last reply 9 Oct 2019, 15:35
    0
    • C Offline
      C Offline
      CKurdu
      wrote on 9 Oct 2019, 15:34 last edited by CKurdu 10 Sept 2019, 15:50
      #2

      I think you should use data.toUInt(&ok, 16). Because you are using hex data.

      You reap what you sow it

      1 Reply Last reply
      7
      • K Ketank16
        9 Oct 2019, 15:17

        Hello, I'm receiving data over serial port like this: CH0:0x20FC0C05and splitting it in form like this: sep: ("CH0:0", "20FC0C05")

        I can split further and copy the data into another string variable data: "20FC0C05". The problem comes when I'm trying to use data.toUInt function. It always returns 0 and I'm not able to figure it out.

        Code snippet:

        if(line != NULL)
                    {
                        //qDebug() << "line: " << line;
                        QStringList sep = line.split('x');
                        QString data;
                        //qDebug() << "sep: " << sep;
                        if(sep.at(0).startsWith("CH0"))
                        {
                            qDebug() << "sep: " << sep;
                            data.append(sep.takeLast());
                            qDebug() << "data: " << data << data.toUInt(&ok, 10) << ok;
                            //qDebug() << "sep: " << sep.takeLast().toInt(&ok,10);
                            //ch0_array[ch0_index] = sep.takeLast().toDouble();
                            //ch0_index += 1;
                            //qDebug() << "ch0: " << ch0_array;
                        }
                    }
        

        Output preview:

        sep:  ("CH0:0", "20FB3500")
        data:  "20FB3500" 0 false
        sep:  ("CH0:0", "20FA040D")
        data:  "20FA040D" 0 false
        sep:  ("CH0:0", "20F88105")
        data:  "20F88105" 0 false
        sep:  ("CH0:0", "20FF240A")
        data:  "20FF240A" 0 false
        sep:  ("CH0:0", "2105C003")
        

        I've also tried toInt and toDouble functions but the result looks the same. If anyone has idea whats going on here, that'll be great.

        P Offline
        P Offline
        Pablo J. Rogina
        wrote on 9 Oct 2019, 15:35 last edited by
        #3

        @Ketank16 said in Error in QString.toUInt/Int/Double : Conversion fails:

        data.toUInt(&ok, 10)

        It looks like you're using the wrong base, don't you? You need to provide the base the value is (i.e. 16 in your example) not the base the value will be converted to. See example in the documentation.

        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

        K 1 Reply Last reply 10 Oct 2019, 09:04
        6
        • P Pablo J. Rogina
          9 Oct 2019, 15:35

          @Ketank16 said in Error in QString.toUInt/Int/Double : Conversion fails:

          data.toUInt(&ok, 10)

          It looks like you're using the wrong base, don't you? You need to provide the base the value is (i.e. 16 in your example) not the base the value will be converted to. See example in the documentation.

          K Offline
          K Offline
          Ketank16
          wrote on 10 Oct 2019, 09:04 last edited by
          #4

          @Pablo-J-Rogina Oh that was pretty quick and thank you. That was a bit confusing for me to understand, but the link you've shared is very useful. Thanks.

          1 Reply Last reply
          0

          1/4

          9 Oct 2019, 15:17

          • Login

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