Hello everybody !
There is something I don't understand, and perhaps could you healp me please ?
My application receive some data from a server, using a QTCpSocket.
Those data are about new mails received by the user on the IMAP service, and my application download the sender, the sent date and the subject of the mail.
The data is stored in a QByteArray (line) in order to be used.
[...]
if (readLineFromSocket(line))
{
[...]
if (QString::fromLatin1(line.
constData()).
startsWith("Subject:")) {
[...]
mail_subject.
append(QString::fromLatin1(line.
constData()));
mail_subject.remove("Subject:");
mail_subject.trimmed();
cout << "SUBJECT : " << mail_subject.toStdString() << endl;
}
}
QByteArray line;
[...]
if (readLineFromSocket(line))
{
[...]
if (QString::fromLatin1(line.constData()).startsWith("Subject:"))
{
[...]
QString mail_subject = "";
mail_subject.append(QString::fromLatin1(line.constData()));
mail_subject.remove("Subject:");
mail_subject.trimmed();
cout << "SUBJECT : " << mail_subject.toStdString() << endl;
}
}
To copy to clipboard, switch view to plain text mode
So here I've my subject :
- stored in a QByteArray (line)
- stored in a QString (mail_subject)
The matter is that sometimes my subject is encoded in UTF-8, and the cout prints something like :
SUBJECT : qualité de
=?UTF-8?B?cXVhbGl0w6kgZGUgbCdhaXIgZHUgMTIvMDQvMjAwNw==
?=
In reality I should read : "SUBJECT : qualité de l'air du 12/04/2007".
So I would like to decode my message. Using the documentation I found QString::fromLocal8Bit and QString::fromUtf8 functions.
QByteArray source
= "=?UTF-8?B?cXVhbGl0w6kgZGUgbCdhaXIgZHUgMTIvMDQvMjAwNw==?=";
cout << "------->" << essais.toStdString() << endl;
cout << "------->" << essai_1.toStdString() << endl;
cout << "------->" << essai_2.toStdString() << endl;
QByteArray source = "=?UTF-8?B?cXVhbGl0w6kgZGUgbCdhaXIgZHUgMTIvMDQvMjAwNw==?=";
QString essai_1 = QString::fromLocal8Bit(source.constData());
QString essai_2 = QString::fromUtf8(source.constData());
cout << "------->" << essais.toStdString() << endl;
cout << "------->" << essai_1.toStdString() << endl;
cout << "------->" << essai_2.toStdString() << endl;
To copy to clipboard, switch view to plain text mode
Here is what I get :
-------> =?UTF-8?B?cXVhbGl0w6kgZGUgbCdhaXIgZHUgMTIvMDQvMjAwNw==?=
-------> =?UTF-8?B?cXVhbGl0w6kgZGUgbCdhaXIgZHUgMTIvMDQvMjAwNw==?=
-------> =?UTF-8?B?cXVhbGl0w6kgZGUgbCdhaXIgZHUgMTIvMDQvMjAwNw==?=
I don't understand how to decode my UTF-8 data .
What's wrong ? Do you understand ?
If I've understand everything, Qt use Unicode. So I should decode from UTF-8 to Unicode ?
.
Bookmarks