PDA

View Full Version : QByteArray::fromBase64() and QTextCodec::toUnicode problem



Mahoney
4th June 2013, 11:00
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:


QByteArray a = QByteArray::fromBase64(src);
// here in 'a' I have decoded text in KOI8-R encoding
QTextCodec *pCodec = QTextCodec::codecForName("koi8-r");
QByteArray b = pCodec->toUnicode(a).toUtf8();
// in 'b' I have garbage instead of desired text


What's wrong with the code?

bibhukalyana
5th June 2013, 12:13
try this.


QTextCodec *codec = QTextCodec::codecForName("KOI8-R");
QTextDecoder *decoder = codec->makeDecoder();

QString string;
while (new_data_available()) {
QByteArray chunk = get_new_data();
string += decoder->toUnicode(chunk);
}
delete decoder;

ChrisW67
5th June 2013, 22:10
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?