[Qt-interest] How to compress QString

Oliver.Knoll at comit.ch Oliver.Knoll at comit.ch
Thu Apr 16 14:37:05 CEST 2009


Jan Kundrát wrote on Thursday, April 16, 2009 1:35 PM:

> Ankit Agarwal wrote:
>> Whats the advantage that we get by converting the string to UTF - 8?
> 
> No need to figure out magic multiplication constants for figuring out
> the string size. 

Isn't the size in bytes of a Unicode string, stored in a QString, not simply:
  
  QString theString = "Whatever";
  uint size = theString.size() * sizeof(ushort); // size in bytes

I don't see any magic constant here ;)

> Basically, if you convert a QString to a QByteArray
> holding its UTF-8 representation, you can tell the size of the data
> from QByteArray's size(). 

I agree that this method is simpler and less errorprone :)

> Also having a stable and well-documented
> intermediate format (which a byte array of UTF-8 data is) always
> helps.     

How is UTF-8 more stable and better documented than UTF-16? I thought the range of Unicode characters is pretty well-defined? Or am I missing something here?

Anyway, as long as you store the data as "qCompressed UTF-16" and read it back as "qUncompressed UTF-16", into a QString, you should be able to send this compressed data even over the network and read/write it even on computers with different endianess (using Qt on both sides). 

Things(tm) might be different (when using UTF-16) if you try to write the data e.g. with a Qt application and read it with a non-Qt application (which somehow has "access" to qUncompress though). But that's probably out of the scope of the OP's question...

Cheers, Oliver

-- 
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22



More information about the Qt-interest-old mailing list