PDA

View Full Version : Returning hex value of utf8 string



Dizgah
31st October 2017, 05:58
I wrote a very simple test code,which receive a utf8 string from QLineEdit and display it in messagebox:


QString txtString = ui->lineEdit->text();
QString scndString = txtString.toUtf8();
QMessageBox msgBox;

msgBox.setText(scndString);
msgBox.exec();

Know I want to display utf8 hex value of string instead of string itself, but when I try to do this :



QString txtString = ui->lineEdit->text();
QString scndString = txtString.toLatin1.toutf8.toHex();
QMessageBox msgBox;

msgBox.setText(scndString);
msgBox.exec();


I received this message:

error: 'txtString.QString::toLatin1' does not have class type QString scndString = txtString.toLatin1.toutf8.toHex();

PierreA
31st October 2017, 11:17
You don't convert it to Latin-1 and then to UTF-8. That doesn't work. You convert the QString (which is UTF-16) directly to UTF-8.

txtString.toUtf8().toHex()
You need the parentheses because toUtf8 is a method (a function which is a member of a class), not a variable. I haven't used QByteArray, but hopefully that'll work.