PDA

View Full Version : Decoding an UTF-8 QByteArray...



Nyphel
24th April 2007, 14:11
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.



QByteArray line;
[...]
if (readLineFromSocket(line))
{
[...]
if (QString::fromLatin1(line.constData()).startsWith("Subject:"))
{
[...]
QString mail_subject = "";
mail_subject.append(QString::fromLatin1(line.const Data()));

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 :


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==?=";

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;


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 ?



.

camel
24th April 2007, 20:22
You suffer the same problem as you did here:
Convert from iso-8859-1 to... Something else :-) (http://www.qtcentre.org/forum/f-qt-programming-2/t-convert-from-iso-8859-1-to-something-else--5951.html)

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 :-)

Nyphel
25th April 2007, 08:03
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 :)