Decoding an UTF-8 QByteArray...
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.
Code:
[...]
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;
}
}
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 :
Quote:
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.
Code:
QByteArray source
= "=?UTF-8?B?cXVhbGl0w6kgZGUgbCdhaXIgZHUgMTIvMDQvMjAwNw==?=";
cout << "------->" << essais.toStdString() << endl;
cout << "------->" << essai_1.toStdString() << endl;
cout << "------->" << essai_2.toStdString() << endl;
Here is what I get :
Quote:
-------> =?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 ?
.
Re: Decoding an UTF-8 QByteArray...
You suffer the same problem as you did here:
Convert from iso-8859-1 to... Something else :-)
What you have in the QByteArray is not UTF-8 but a (probably RFC 2045 too?) encoding of UTF-8.
You will have to convert this one first too.
Have a nice Day :-)
Re: Decoding an UTF-8 QByteArray...
I understood my problem : the "B" in "=?UTF-8?B?" stand for "Base64".
So I have to decode from Base64 in order to obtain an UTF-8 QByteArray,
and then the fromUtf8() function works well :)