Results 1 to 3 of 3

Thread: Decoding an UTF-8 QByteArray...

  1. #1
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

    Qt Code:
    1. [...]
    2. if (readLineFromSocket(line))
    3. {
    4. [...]
    5. if (QString::fromLatin1(line.constData()).startsWith("Subject:"))
    6. {
    7. [...]
    8. QString mail_subject = "";
    9. mail_subject.append(QString::fromLatin1(line.constData()));
    10.  
    11. mail_subject.remove("Subject:");
    12. mail_subject.trimmed();
    13. cout << "SUBJECT : " << mail_subject.toStdString() << endl;
    14. }
    15. }
    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.

    Qt Code:
    1. QByteArray source = "=?UTF-8?B?cXVhbGl0w6kgZGUgbCdhaXIgZHUgMTIvMDQvMjAwNw==?=";
    2.  
    3. QString essai_1 = QString::fromLocal8Bit(source.constData());
    4. QString essai_2 = QString::fromUtf8(source.constData());
    5.  
    6. cout << "------->" << essais.toStdString() << endl;
    7. cout << "------->" << essai_1.toStdString() << endl;
    8. 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 ?



    .
    Last edited by Nyphel; 24th April 2007 at 14:23.

  2. #2
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

  3. #3
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default 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

Similar Threads

  1. Can QByteArray takes place of BYTE?
    By hiuao in forum Qt Programming
    Replies: 2
    Last Post: 5th April 2007, 11:49
  2. QDomElement to QByteArray ?
    By probine in forum Qt Programming
    Replies: 3
    Last Post: 2nd May 2006, 17:01
  3. How to display a QByteArray as an Image ?
    By probine in forum Newbie
    Replies: 7
    Last Post: 3rd April 2006, 13:45
  4. Reading QByteArray from QDataStream Qt3.3
    By high_flyer in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2006, 20:23
  5. How to read a Number from the QByteArray
    By antonio.r.tome in forum Qt Programming
    Replies: 2
    Last Post: 24th February 2006, 15:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.