PDA

View Full Version : strange conversion!



navid
14th June 2010, 10:03
hi

this is code:



void test(void)
{
QByteArray _qba;
_qba.clear();
int index=0;
unsigned char hi,lo;
unsigned int uin,u32r;
bool br;
for(int i=0; i<dock->tableWidget->rowCount(); i++){
u32r=dock->tableWidget->item(i,2)->text().toUInt(&br);
qDebug()<<"["<<u32r<<"]";
if(!br)return;
hi=(unsigned char)(u32r/ 256);
lo=(unsigned char)(u32r % 256);
_qba.append(hi);
_qba.append(lo);
qDebug()<<"("<<(unsigned char)_qba.data()[index]<<","<<(unsigned char)_qba.data()[index+1]<<")";
uin=(unsigned int)(_qba.data()[index]*256+_qba.data()[index+1]);
index+=2;
uin &= 0x0000ffff;
qDebug()<<uin;
qDebug()<<"{"<<hi*256+lo<<"}";
}
}

this is debug:
[ 100 ]
( 0 , 100 )
100
{ 100 }
[ 350 ]
( 1 , 94 )
350
{ 350 }
[ 1400 ]
( 5 , 120 )
1400
{ 1400 }
[ 36344 ]
( 141 , 248 )
36088
{ 36344 }
[ 62000 ]
( 242 , 48 )
62000
{ 62000 }
[ 58344 ]
( 227 , 232 )
58088
{ 58344 }
[ 0 ]
( 0 , 0 )
0
{ 0 }


why there is strange conversion for some ones:
[ 36344 ]
( 141 , 248 )
36088
{ 36344 }

also, using qDebug()<<(int)uin; produce same result!

high_flyer
14th June 2010, 10:33
The operation:

lo=(unsigned char)(u32r % 256);
is not doing what you want.
If u32r ist 257 for example, lo = 0 as a result in your code, which is false, it should be 1, or 0x01.
You also have other principal mistakes such as:
you are using a 32 bit vars, but only test their lower 8 and 16 bits.
It is possible that in you example the values really didn't go above 0xFFFF, but if they will, your code wont work even if correct hi/low dissection of the lower 8 and 16 bits.
Try the following:


//This code will only disect 16 bit values, for 32 values you will have to change the code in a similar way
for(unsigned short i=0; i<dock->tableWidget->rowCount(); i++){
unsigned short u16r=dock->tableWidget->item(i,2)->text().toUInt(&br);
qDebug()<<"["<<u16r<<"]";
if(!br)return;
hi=(unsigned char)(u16r>>8);
lo=(unsigned char)(u16r);
...
}



Note I didn't test this code for compilation, typos might be present, its just pseudo code to explain the concept, yet the code might compile ok.

navid
14th June 2010, 10:43
because of developement codes for 32bits ones, I declared 'unsigned int'.
as we see in debug text, the problem exist in 'bytes' to 'unsigned int' conversion, where [ 36344 ] or ( 141 , 248 ) are converted to 36088 !
141x256+248=36344 or 8DF8
while 36088 is 8CF8
it shows that there is one decrease in high byte!
is it a bug in compiler?

navid
14th June 2010, 10:46
also, I have tested with bits shift operators, unfortunatly the result is the same
>>8 for / 256
<<8 for * 256

high_flyer
14th June 2010, 10:58
because of developement codes for 32bits ones, I declared 'unsigned int'.
As I said, you have to adapt the high low dissection to match 32 bits.


is it a bug in compiler?
No, its a bug in your code.


also, I have tested with bits shift operators, unfortunately the result is the same
Show your code.

navid
14th June 2010, 11:08
uin=(((unsigned int)_qba.data()[index])<<8)+(unsigned int)(_qba.data()[index+1]);

navid
14th June 2010, 11:13
If u32r ist 257 for example, lo = 0 as a result in your code, which is false, it should be 1, or 0x01.

as the code shows ( lo=(unsigned char)(u32r % 256); ), reminder of 257 over 256 is 1 and so lo=1;

high_flyer
14th June 2010, 11:34
as the code shows ( lo=(unsigned char)(u32r % 256); ), reminder of 257 over 256 is 1 and so lo=1;
That is true, it was my mistake ,sorry. (I had the float result in mind).

Lets start again:
What is it you want to achieve?
If you have have 32 bit values, then what you are doing makes little sense.

If you do the dissecting the way I showed you, it will work.
Try also to make your code a bit more easy to read, it hard to follow, and it is more prone for mistakes.