[Qt-interest] unsigned char

Ian Thomson Ian.Thomson at iongeo.com
Tue Nov 3 18:17:59 CET 2009


Hi,

Those are signed chars. If you use the data() function of QByteArray, 
you can cast it to unsigned char like this:

unsigned char* ublock = static_cast<unsigned char*>(block->data());
qDebug() << QString::number(ublock[11], 16);

I believe that should work.

Cheers,
Ian.

Shaun van Wyngaard (Home) wrote:
> I have recently had the "misfortune" to encounter unsigned char usage, 
> and need some help on it. If this is indeed what it is.
> Specifically, I am reading some values from QTcpSocket into a QByteArray 
> by means of the following code.
>  
> QByteArray block;
> block = tcpSocket->readAll();
>  
> where tcpSocket is of type QTcpSocket.
> Now when I do a debug run using watchpoints, the values within block are 
> as expected, specifically I am looking at the following indices, with 
> the associated values as shown in the local variables being monitored.
>  
> block[9] : 3d
> block[10] : 7f
> block[11] : bf
> block[12] : 26
>  
> However, when I try print them using
>  
> qDebug() << QString 
> <http://doc.trolltech.com/latest/QString.html>::number(block[9],16);
> qDebug() << QString 
> <http://doc.trolltech.com/latest/QString.html>::number(block[10],16);
> qDebug() << QString 
> <http://doc.trolltech.com/latest/QString.html>::number(block[11],16);
> qDebug() << QString 
> <http://doc.trolltech.com/latest/QString.html>::number(block[12],16);
>  
>  I get the following output:
>  
> "3d"
> "7e"
> "ffffffffffffffbf"
> "26"
>  
> A workaround to this is to check if the value is less than zero, and if 
> yes, then to add 255 (if I remember what I saw earlier), and that will 
> revert it back to its original value, in this case the third value being 
> the one needing attention. This I found after searching for awhile. Now 
> I have an idea that QByteArray should be able to handle this situation 
> automatically, I just can't seem to figure out the code. Could someone 
> help me please?
>  
> Thanks,
> Shaun
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest




More information about the Qt-interest-old mailing list