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
Developer
YamilBracho
Posts: 710
Registered: ‎05-31-2010
My Carrier: Movistar
Accepted Solution

Troube with Base64 and qCompress/qUncompress

I need to compress and encoded a QString in base 64. I Wrote 2 methods for that task. Those methods are :

/*
* Compress a QString and converts to Base 64
*/
QString BlackBerry10Utils::compressAndBase64(const QString &paramString)
{
	QByteArray byteArray;
    byteArray.append(paramString);

	byteArray = qCompress(byteArray);
	byteArray.toBase64();
	byteArray.remove(0, 4);

	QString result = QString(byteArray);

	return result;
}

/**
 * Converts from Base 64 and un-compress a QString
 */
QString BlackBerry10Utils::base64AndUnCompress(const QString &paramString)
{
	QByteArray byteArray;
    byteArray.append(paramString);

    byteArray = QByteArray::fromBase64(byteArray);
    byteArray = qUncompress(byteArray);

    QString result = QString(byteArray);

	return result;

}

 

And my test code :

QString str("Hello, World!");
str = BlackBerry10Utils::compressAndBase64(str);

 

str = BlackBerry10Utils::base64AndUnCompress(str);

 

However the process does not return str as its original value... 

I used this same method in Android, Java in BlackBerry and WindowsPhone and works fine...

 

 

 

Please use plain text.
Developer
Zmey
Posts: 1,494
Registered: ‎12-18-2012

Re: Troube with Base64 and qCompress/qUncompress

In base64AndUnCompress there's a line:

QString result = QString(byteArray);

 

This is equivalent of calling QString::fromAscii() which won't work correctly for most encodings.

 

Try replacing it with:

QString result = QString::fromUtf8(byteArray.data(), byteArray.size());

It's worth doing the same in compress function too, but it's not important for base64-encoded data as it's not using 8th bit. 

 

In compressAndBase64 replace

	QByteArray byteArray;
    byteArray.append(paramString);

 with

QByteArray byteArray(paramString.toUtf8());

 


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Developer
YamilBracho
Posts: 710
Registered: ‎05-31-2010
My Carrier: Movistar

Re: Troube with Base64 and qCompress/qUncompress

That was!!!

I forget to encode to utf8 as I did in the other app versions..

Thanks a lot Zmey...!!

Please use plain text.