[Qt-interest] Qfile.readAll() returning signed quint64-qbytearray

Thiago Macieira thiago at kde.org
Mon Nov 1 16:42:32 CET 2010


On Monday, 1 de November de 2010 03:57:56 Joakim Saxin wrote:
> Sorry for the weird topic but I am confused.
> 
> Basically, file.readAll() returns a QByteArray. Now, if a byte in file is
> has MSB set, QFile seems to interpret this as as signed value, and so uses
> two-complement conversion to the 64-bit that I have most bitterly learned
> is what QByteArray actually stores.
> 
> Because of this, certain values held byte QByteArray, a datastructure you
> would figure indexes single bytes, are in the neighbourhood
> 1.84467440737e19 unless treated as signed.
> 
> I am tempted to report this as a bug but must suspect there is something I
> am doing wrong (and have been for quite a while). Does anyone know?

You're doing something wrong...

QFile doesn't interpret anything. It just reads the bytes off the file and 
places them in QByteArray. On its turn, QByteArray doesn't interpret or modify 
the bytes in any way: it simply stores them.

Now, QByteArray's element type is "char", which is signed in most platforms. 
Your problem is probably caused by your integer arithmethic: a signed char or 
signed short is sign-extended to a signed int before any operations (i.e., 
0xc0 becomes 0xffffffc0). You may need to cast the elements to uchar before 
operating on them -- uchars and ushorts are zero-extended to signed int.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Senior Product Manager - Nokia, Qt Development Frameworks
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20101101/02bdbc7e/attachment.bin 


More information about the Qt-interest-old mailing list