QVariant(QString) to hex QByteArray
-
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?
-
@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());
-
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.