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. [SOLVED] String to double conversion failed and issues with qDebug
QtWS25 Last Chance

[SOLVED] String to double conversion failed and issues with qDebug

Scheduled Pinned Locked Moved General and Desktop
16 Posts 3 Posters 4.6k Views
  • 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
    andreyc
    wrote on last edited by
    #5

    What is a result of std::cout instead of qDebug()
    What is a result of using QByteArray as intermediate variable ?
    @
    QByteArray data = lat_mid.toLatin1();
    qDebug() << data;
    qDebug() << data.data();
    @

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      QG90
      wrote on last edited by
      #6

      [quote author="andreyc" date="1409010147"]What is a result of std::cout instead of qDebug()[/quote]

      Don't know how to make this work. Even @std::cout << "asdf" << endl;@
      doesn't show up anywhere. But it built.

      [quote author="andreyc" date="1409010147"]What is a result of using QByteArray as intermediate variable ?
      @
      QByteArray data = lat_mid.toLatin1();
      qDebug() << data;
      qDebug() << data.data();
      @
      [/quote]

      The result is "4 for the first and 4 for the second

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andreyc
        wrote on last edited by
        #7

        What about
        @
        QByteArray data = lat_mid.toLocal8Bit();
        @

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andreyc
          wrote on last edited by
          #8

          Scratch my previous suggestions.
          I think you need to write your own deserializer.

          Something simple like skipping each other byte in a for loop.
          Or using QByteArray and QDataStream
          @
          class MyData
          {
          ...
          QString toString() const;
          };
          QDataStream &operator>>(QDataStream &in, MyData &data);

          QByteArray data = QByteArray::fromRawData(usb_data.constData(), 60);
          QDataStream in(&data, QIODevice::ReadOnly);
          MyData data;
          in >> data;
          qDebug() << data.toString();
          @

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andreyc
            wrote on last edited by
            #9

            And one more attempt.

            I guess that usb_data.constData() returns const char*
            @
            QString qmessage = QString::fromUtf16(reinterpret_cast<const unsigned short*>(usb_data.constData()));
            @

            [EDIT] Example
            @
            const char* latStr = "\x34\x00\x30\x00\x34\x00\x35\x00\x2e\x00\x38\x00\x39\x00\x32\x00\x31\x00";
            QString str1 = QString::fromUtf16(reinterpret_cast<const unsigned short*>(latStr));
            qDebug() << str1;
            @
            output
            @
            "4045.8921"
            @

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #10

              By the way, what class is usb_data ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                QG90
                wrote on last edited by
                #11

                [quote author="andreyc" date="1409012176"]What about
                @
                QByteArray data = lat_mid.toLocal8Bit();
                @[/quote]

                Still the same "4 and 4. I'll have a look at your other suggestion.

                [quote author="SGaist" date="1409038961"]By the way, what class is usb_data ?[/quote]

                It's Qbytearray.

                1 Reply Last reply
                0
                • Q Offline
                  Q Offline
                  QG90
                  wrote on last edited by
                  #12

                  [quote author="andreyc" date="1409020154"]And one more attempt.

                  I guess that usb_data.constData() returns const char*
                  @
                  QString qmessage = QString::fromUtf16(reinterpret_cast<const unsigned short*>(usb_data.constData()));
                  @

                  [EDIT] Example
                  @
                  const char* latStr = "\x34\x00\x30\x00\x34\x00\x35\x00\x2e\x00\x38\x00\x39\x00\x32\x00\x31\x00";
                  QString str1 = QString::fromUtf16(reinterpret_cast<const unsigned short*>(latStr));
                  qDebug() << str1;
                  @
                  output

                  "4045.8921"

                  [/quote]

                  I used your example @QString str1 = QString::fromUtf16(reinterpret_cast<const unsigned short*>(lat_mid));
                  qDebug() << str1;@

                  but i get an error @error C2440: 'reinterpret_cast' : cannot convert from 'QString' to 'const unsigned short *'@

                  1 Reply Last reply
                  0
                  • Q Offline
                    Q Offline
                    QG90
                    wrote on last edited by
                    #13

                    Also this is how usb_data is created @QByteArray usb_data(reinterpret_cast<char*>(buf), sizeof(buf));@

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andreyc
                      wrote on last edited by
                      #14

                      [quote author="QG90" date="1409047489"]
                      I used your example
                      @
                      QString str1 = QString::fromUtf16(reinterpret_cast<const unsigned short*>(lat_mid));
                      qDebug() << str1;
                      @
                      but i get an error
                      @
                      error C2440: 'reinterpret_cast' : cannot convert from 'QString' to 'const unsigned short *'
                      @
                      [/quote]

                      It want work. If I understand it correctly you are getting UTF16 string from USB. UTF16 contains zeros, not number 0 but ascii code 0. And it will break any standard string operations.
                      So, if you prefer to use standard string operations (QString etc) you need to translate you incoming buffer to a regular string.

                      [quote author="QG90" date="1409054635"]
                      Also this is how usb_data is created @QByteArray usb_data(reinterpret_cast<char*>(buf), sizeof(buf));@
                      [/quote]

                      Instead of converting buf to QByteArray convert it to QString
                      @
                      QString usbStr;
                      usbStr = QString::fromUtf16(reinterpret_cast<const unsigned short*>(buf), sizeof(buf));
                      @

                      And then
                      @
                      QString latitude = usbStr.mid(0,21);
                      QString longitude = usbStr.mid(23,25);
                      QString height = usbStr.mid(50);

                      QString lat_mid = usbStr.mid(0,18);
                      QString lon_mid = usbStr.mid(23,21);

                      double lat = lat_mid.toDouble();
                      double lon = lon_mid.toDouble();

                      qDebug() << lat << latitude;
                      @

                      1 Reply Last reply
                      0
                      • Q Offline
                        Q Offline
                        QG90
                        wrote on last edited by
                        #15

                        It seems to work!!!! Now on qDebug i receive 4045.88 "4045.8783,N"
                        probably the 783 part on the number sums to an 8.

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          andreyc
                          wrote on last edited by
                          #16

                          Yep, looks like legitimate rounding.
                          I think qDebug() does rounding because I don't see any rounding rules in QString::toDouble().

                          If you consider this thread as solved then please add [SOLVED] to the title of your original post.

                          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