Evans Belloeil
Evans Belloeil

Reputation: 2503

Convert QString hexadecimal to ASCII value

My goal is to convert an hexadecimal value which is contained in a QString to its ASCII value.

I have :

QString hexaValue = receiveText.left(14); // receive texte is another QString

My problem here, is that I have my hexadecimal value in a Qstring and not in a QByteArray, so all of the solutions that I found are not working, I try to call .data() or fromHex() , but this ain't working here, because I'm forced to used a QString and not a QByteArray

Should I convert my QString to a QByteArray, is there a simple solution ?

Upvotes: 2

Views: 5127

Answers (1)

Arpegius
Arpegius

Reputation: 5887

You can just use QString::toLatin1to convert hex string to QByteArray and to convert it back to QString use either QString::fromLocal8Bit for local encoding or QString::fromUtf8 if your hex encoded string are in UTF8.

QString hexaValue = receiveText.left(14); // received text is another QString
QString textValue = QString::fromLocal8Bit(QByteArray::fromHex(hexaValue.toLatin1()));

Upvotes: 4

Related Questions