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. QVariant(QString) to hex QByteArray
Forum Updated to NodeBB v4.3 + New Features

QVariant(QString) to hex QByteArray

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 3.1k Views 1 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.
  • B Offline
    B Offline
    bdmontz
    wrote on last edited by
    #1

    So I know there must be a simple way to do this, but I keep going in circles or finding dead ends.

    I saved an integer value from a LineEdit that ranges from 0 to 255. I used a QIntValidator to make sure of this. The values were saved to the registry using QSettings.

    I can load the value from the registry [qdebug shows "QVariant(QString,"0")] but now I need to load that value into a QByteArray for transmission over UART. so a "0" should be "\x00" and a "255" should be "\xFF". I just can't connect the pieces to make it work. I've looked through the QString::number, QByteArray::number functions, the various other conversion function, I'm just not getting it. The closest I've gotten was "ff" which is two bytes and just doesn't work.

    I'm at the point where I might just write my own function for this stupid conversion. Qt seems great for high level stuff, but low level manipulation always seems tedious and painful. Is there a straightforward way to take care of this?

    kshegunovK 1 Reply Last reply
    0
    • B bdmontz

      So I know there must be a simple way to do this, but I keep going in circles or finding dead ends.

      I saved an integer value from a LineEdit that ranges from 0 to 255. I used a QIntValidator to make sure of this. The values were saved to the registry using QSettings.

      I can load the value from the registry [qdebug shows "QVariant(QString,"0")] but now I need to load that value into a QByteArray for transmission over UART. so a "0" should be "\x00" and a "255" should be "\xFF". I just can't connect the pieces to make it work. I've looked through the QString::number, QByteArray::number functions, the various other conversion function, I'm just not getting it. The closest I've gotten was "ff" which is two bytes and just doesn't work.

      I'm at the point where I might just write my own function for this stupid conversion. Qt seems great for high level stuff, but low level manipulation always seems tedious and painful. Is there a straightforward way to take care of this?

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @bdmontz said in QVariant(QString) to hex QByteArray:

      Qt seems great for high level stuff, but low level manipulation always seems tedious and painful.

      Qt is a library for C++, so anything you can do with C++ you can do the same or easier with Qt.

      Is there a straightforward way to take care of this?

      Yes.

      QVariant value(QStringLiteral("0"));
      
      QByteArray data;
      QDataStream out(&data);
      out << static_cast<qint8>(value.toInt());
      

      Read and abide by the Qt Code of Conduct

      B 1 Reply Last reply
      1
      • kshegunovK kshegunov

        @bdmontz said in QVariant(QString) to hex QByteArray:

        Qt seems great for high level stuff, but low level manipulation always seems tedious and painful.

        Qt is a library for C++, so anything you can do with C++ you can do the same or easier with Qt.

        Is there a straightforward way to take care of this?

        Yes.

        QVariant value(QStringLiteral("0"));
        
        QByteArray data;
        QDataStream out(&data);
        out << static_cast<qint8>(value.toInt());
        
        B Offline
        B Offline
        bdmontz
        wrote on last edited by
        #3

        Sorry, I'm not very familiar with QDataStreams... I get an error "error: C2664: 'QDataStream::QDataStream(QIODevice *)' : cannot convert parameter 1 from 'QByteArray *' to 'QIODevice *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast"

        not really sure what that means.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          bdmontz
          wrote on last edited by
          #4

          google is my friend.

          QByteArray data;
          QBuffer buffer(&data);
          buffer.open(QIODevice::WriteOnly);
          QDataStream out(&buffer);
          out << static_cast<qint8>(Delay.toInt());
          qDebug() << "QByteArray: " << data;
          

          Thanks for the help kshegunov!

          kshegunovK 1 Reply Last reply
          0
          • B bdmontz

            google is my friend.

            QByteArray data;
            QBuffer buffer(&data);
            buffer.open(QIODevice::WriteOnly);
            QDataStream out(&buffer);
            out << static_cast<qint8>(Delay.toInt());
            qDebug() << "QByteArray: " << data;
            

            Thanks for the help kshegunov!

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #5

            @bdmontz
            Sorry my bad. It should've been:

            QDataStream out(&data, QIODevice::WriteOnly);
            

            Read and abide by the Qt Code of Conduct

            B 1 Reply Last reply
            1
            • kshegunovK kshegunov

              @bdmontz
              Sorry my bad. It should've been:

              QDataStream out(&data, QIODevice::WriteOnly);
              
              B Offline
              B Offline
              bdmontz
              wrote on last edited by
              #6

              yep, that works too!

              kshegunovK 1 Reply Last reply
              0
              • B bdmontz

                yep, that works too!

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #7

                It's a shorthand for not creating a QBuffer and it only works with QByteArray, because that's a very common requirement.

                Read and abide by the Qt Code of Conduct

                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