[Qt-interest] unsigned char
BRM
bm_witness at yahoo.com
Tue Nov 3 18:53:33 CET 2009
You also have a couple other options.
The extended data you are seeing is basically b/c it is getting sign-extended.
One option is to just mask for the bits you want (e.g. value & 0x000000FF).
Another option is to use QByteArray and QString to generate Hex dumps of the data:
qDebug()<<QString(block.toHex());
Personally, I prefer the toHex() variation, though I also tend to do as follows:
QString hexData(block.toHex())
qDebug("%s", hexData.toStdString().c_str());
But that's just more or less personal preference. in the end, it's really doing the same thing.
Ben
----- Original Message ----
From: Ian Thomson <Ian.Thomson at iongeo.com>
To: qt-interest at trolltech.com
Sent: Tue, November 3, 2009 12:17:59 PM
Subject: Re: [Qt-interest] unsigned char
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
_______________________________________________
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