[Qt-interest] How to compress QString
Oliver.Knoll at comit.ch
Oliver.Knoll at comit.ch
Thu Apr 16 11:08:47 CEST 2009
Iurii Gordiienko wrote on Thursday, April 16, 2009 10:36 AM:
> Hi
>
> How to compress-uncompress the QString using the qCompress and
> qUncompress if I need to support the unicode strings? Qt-4.5.0
- Compress
One flavour of qCompress takes an uchar * and the number of bytes as argument:
QByteArray qCompress (const uchar *data, int nbytes, int compressionLevel = -1)
You get access to the raw unicode bytes within a QString with several methods:
http://doc.trolltech.com/4.5/qstring.html#data-2 (you get a QChar *)
http://doc.trolltech.com/4.5/qstring.html#utf16 (you get a ushort *)
http://doc.trolltech.com/4.5/qstring.html#unicode (you get a QChar *)
The number of bytes should also be fairly simple to figure out, something like QString::size() * 2 or so. After some QChar->unicode() and some casting to uchar * you should be able to succesfully pass this data to qCompress.
- Decompress
qUncompress always returns a raw QByteArray. Given that data you should be able to put it as unicode into a QString:
http://doc.trolltech.com/4.5/qstring.html#setUnicode
http://doc.trolltech.com/4.5/qstring.html#setUtf16
Again you need to figure out the 'size' (number of characters! not number of bytes this time) of the unicode contained in QByteArray and pass this to either method above as well. And you need to think a bit about how you figure out whether the uncompressed QByteArray really contains unicode:
- You explicitly encode this in your compressed stream or
- "You simply know"
Cheers, Oliver
--
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22
More information about the Qt-interest-old
mailing list