Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
New Developer
Posts: 10
Registered: ‎12-08-2013
My Device: Z10
My Carrier: *

Vcard2.1 recieved from android decoded quoted printable problem

hi,

i have a problem in inserting the correct UTF-8 decoded string into a QString.

 

the string i get from An android phone is:

=42=75=62=69=D7=A2=D7=A2=D7=A2=D7=A2

which stands for bubiעעע

in the momentics ide i see the string correctly but using some online tools i saw a problem with that android encoding..

 

after decode i used  the following:

QStringArray data = quotedPrintable::decode(string)

 

QTextCodec *codec2 = QTextCodec::codecForName("UTF-8");

QString utfStr = codec2->toUnicode(data);

 

i have also tried using qtextstream:

 QTextCodec *codec = QTextCodec::codecForName("GBK");

   QString uc =QTextCodec::codecForName( "GBK")->toUnicode(decodedData);

   QString utfStr;

   QTextStream streamFileOut(&uc);

   streamFileOut.setCodec("UTF-8");

   streamFileOut >> utfStr;

 

 

DOES ANYONE ANCOUTERED DECODE PROBLEM FROM ANDROID PHONES?

IS IT ENDIANITY PROBLEM? (i saw i have to revers both endianity and the low 4 bytes with the high one`s in each block which does not make sense.. 

 

i decode the quoted printable string i get using the following decode code: 

 

QByteArray& QuotedPrintable::decode(const QString &input)

{

 

    //                    0  1  2  3  4  5  6  7  8  9  :  ;  <  =  >  ?  @  A   B   C   D   E   F

    const int hexVal[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 14, 15};

 

    QByteArray *output = new QByteArray();

    QString* anotherOutput = new QString();

 

    for (int i = 0; i < input.length(); ++i)

    {

        if (input.at(i).toAscii() == '=')

        {

        //ushort character = (hexVal[input.at(++i).toAscii() - '0'] << 4) + hexVal[input.at(++i).toAscii() - '0'];

        ushort character = (hexVal[input.at(++i).toAscii() - '0'] << 4) + hexVal[input.at(++i).toAscii() - '0'];

 

            output->append(character);

 

 

            QString temp(character);

            anotherOutput->append(*temp.unicode());

        }

        else

        {

            output->append(input.at(i).toAscii());

 

        short character = input.at(i).toAscii();

        character <<= 8;

        character += input.at(i + 1).toAscii();

        /////++i;

        //////////character += input.at(i).toAscii();

 

            output->append(input.at(i).toAscii());

            ///////////QString temp;

            anotherOutput->append(character);

        }

    }

 

    return *output;

    /*

        QTextCodec *codec = QTextCodec::codecForName("UTF-8");

          if (codec)

          {

              rv = codec->toUnicode(buf);

          }

          return rv;

          */

}

i have also tried this decode:

 

    QString rv;

    QByteArray buf;

        QByteArray src=input.toLatin1();

        int len = src.length();

        for (int i = 0; i < len; i++)

        {

            if (src[i] == '_')

            {

                buf += 0x20;

            }

            else if (src[i] == '=')

            {

                if (i+2 < len)

                {

                    buf += QByteArray::fromHex(src.mid(i+1,2));

                    i += 2;

                }

            }

            else

            {

                buf += src[i];

            }

        }

 

IS IT SOME KIND OF ENDIANITY PROBLEM?

IF YES, PLEASE HELP!!

ITZIK

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Vcard2.1 recieved from android decoded quoted printable problem

If QStringArray is a typo and data is actually a QByteArray,

QString s = QString::fromUtf8(data.data(), data.size());

should produce the correct string.

Andrey Fidrya, @zmeyc on twitter
New Developer
Posts: 10
Registered: ‎12-08-2013
My Device: Z10
My Carrier: *

Re: Vcard2.1 recieved from android decoded quoted printable problem

hi,

thanks, actually i have already tried that and to get the utf8 from QObject - did not work..

if you have any additional ideas i would be most glad to try...

can it be some kind of endianity problem?

thanks in advance,

itzik

 

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Vcard2.1 recieved from android decoded quoted printable problem

[ Edited ]

The function was broken. It was probably based on this project, but it's also broken:

https://github.com/bluetiger9/Qt-Quoted-Printable/blob/master/quotedprintable.cpp

 

I've fixed the errors (logic, memory leaks, potential buffer overflow etc) and it now produces the expected results.

 

I've changed the parameter type to QByteArray. Most likely the data will be initially in QByteArray so there's no sense in converting it to QString and back, but this can be easily changed if needed.

 

upd: Qt classes such as QString or QByteArray are lazily copied and can be passed by value, it's lightweight operation.

"X *x = new X(); return *x;" leaks memory.

 

QByteArray decode(const QByteArray &input)
{
	//                              0  1  2  3  4  5  6  7  8  9  :  ;  <  =  >  ?  @  A   B   C   D   E   F
	const unsigned char hexVal[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 14, 15};

	QByteArray output;
	output.reserve(input.size());

	int len = input.size();
	for (int i = 0; i < len; ++i)
	{
		if (input[i] == '=' && i + 2 < len)
		{
			unsigned i1 = input[++i] - '0';
			unsigned i2 = input[++i] - '0';
			if (i1 < sizeof(hexVal) / sizeof(hexVal[0]) &&
					i2 < sizeof(hexVal) / sizeof(hexVal[0]))
			{
				unsigned char c = (hexVal[i1] << 4) + hexVal[i2];
				output.append(c);
			}
		}
		else
		{
			output.append(input[i]);
		}
	}

	return output;
}

int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);

	QByteArray src("=42=75=62=69=D7=A2=D7=A2=D7=A2=D7=A2");
	QByteArray decodedData = decode(src);
	QString s = QString::fromUtf8(decodedData);

	qDebug() << "Result" << s;

	return a.exec();
}

 

Result "Bubiעעעע" 

 

 


Andrey Fidrya, @zmeyc on twitter
Highlighted
New Developer
Posts: 10
Registered: ‎12-08-2013
My Device: Z10
My Carrier: *

Re: Vcard2.1 recieved from android decoded quoted printable problem

hi Zmey,

thank you very much for you comments! - i have tested it using the address builder and it works fine.

i have used that option but i was looking at the memory viewer in the ide and it showed (and still show) wrong encoding even after switching to utf-8 represantation..

in any case it works perfectly !

thanks again!