[Qt-interest] unsigned char

BRM bm_witness at yahoo.com
Tue Nov 3 19:47:48 CET 2009


Yes, you should be able to do something like the following:

qDebug()<<QString(QByteArray(block[12]).toHex());

I think that's right...check the usage of QByteArray. block[12] just returns a reference into the middle of the data block.
Really you want to cut out the byte or so that you're interested in, push that into a QByteArray of its own, and then
do the conversion. A little more work (in code) but well worth it.

Ben



----- Original Message ----
From: Shaun van Wyngaard (Home) <shaungvw at gmail.com>
To: BRM <bm_witness at yahoo.com>; qt-interest at trolltech.com; Shaun <shaunv at pdc.co.za>
Sent: Tue, November 3, 2009 1:18:25 PM
Subject: Re: [Qt-interest] unsigned char

Thanks, I tried masking, but without success. It is a 64 bit number that I 
need to mask to, and so I tried
value ^ 0xFFFFFFFFFFFFFF00 (XOR), but battled because of the size needed.
But now that you show the masking done by ANDing, I guess value & 0xFF 
should also work?

Pity that QByteArray doesn't have a simpler way of doing this, without 
having to manually type cast it, or mask it.
Maybe like "unsigned QByteArray block"?

With regards to the qDebug()<<QString(block.toHex()), would this output the 
whole array, or would I do what needed by
qDebug()<<QString(block[12].toHex()) for say index 12?
Sorry, I can't test myself as I am away from my work computer right now, but 
I quite like this way.

Regards,
Shaun

--------------------------------------------------
From: "BRM" <bm_witness at yahoo.com>
Sent: Tuesday, November 03, 2009 7:53 PM
To: <qt-interest at trolltech.com>
Subject: Re: [Qt-interest] unsigned char

> 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
>
> _______________________________________________
> 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