Results 1 to 3 of 3

Thread: QByteArray::fromBase64() and QTextCodec::toUnicode problem

  1. #1
    Join Date
    Jun 2013
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default QByteArray::fromBase64() and QTextCodec::toUnicode problem

    Hi. I'm parsing an email message headers. The strings are encoded with Base64 algoritm and are in KOI8-R codepage.
    I do the following:
    Qt Code:
    1. QByteArray a = QByteArray::fromBase64(src);
    2. // here in 'a' I have decoded text in KOI8-R encoding
    3. QTextCodec *pCodec = QTextCodec::codecForName("koi8-r");
    4. QByteArray b = pCodec->toUnicode(a).toUtf8();
    5. // in 'b' I have garbage instead of desired text
    To copy to clipboard, switch view to plain text mode 

    What's wrong with the code?

  2. #2
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QByteArray::fromBase64() and QTextCodec::toUnicode problem

    try this.
    Qt Code:
    1. QTextCodec *codec = QTextCodec::codecForName("KOI8-R");
    2. QTextDecoder *decoder = codec->makeDecoder();
    3.  
    4. QString string;
    5. while (new_data_available()) {
    6. QByteArray chunk = get_new_data();
    7. string += decoder->toUnicode(chunk);
    8. }
    9. delete decoder;
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QByteArray::fromBase64() and QTextCodec::toUnicode problem

    How are you determining that there is garbage in b and that the garbage is not in a or src?
    Short example inputs and expected/actual outputs?

Similar Threads

  1. QIODevice and QTextCodec?
    By whitefurrows in forum Qt Programming
    Replies: 5
    Last Post: 26th November 2010, 18:50
  2. QByteArray Problem
    By foxhengxing in forum Qt Programming
    Replies: 1
    Last Post: 18th August 2010, 03:04
  3. QTextCodec bad encoding?
    By ComaWhite in forum Newbie
    Replies: 0
    Last Post: 18th September 2009, 06:15
  4. Question in QTextCodec.
    By morgana in forum Newbie
    Replies: 1
    Last Post: 21st September 2008, 16:29
  5. QTextCodec::setCodecForCStrings() in libraries
    By conexion2000 in forum Qt Programming
    Replies: 5
    Last Post: 10th August 2007, 21:21

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.