Results 1 to 8 of 8

Thread: strange conversion!

  1. #1
    Join Date
    Oct 2009
    Posts
    90
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default strange conversion!

    hi

    this is code:

    Qt Code:
    1. void test(void)
    2. {
    3. QByteArray _qba;
    4. _qba.clear();
    5. int index=0;
    6. unsigned char hi,lo;
    7. unsigned int uin,u32r;
    8. bool br;
    9. for(int i=0; i<dock->tableWidget->rowCount(); i++){
    10. u32r=dock->tableWidget->item(i,2)->text().toUInt(&br);
    11. qDebug()<<"["<<u32r<<"]";
    12. if(!br)return;
    13. hi=(unsigned char)(u32r/ 256);
    14. lo=(unsigned char)(u32r % 256);
    15. _qba.append(hi);
    16. _qba.append(lo);
    17. qDebug()<<"("<<(unsigned char)_qba.data()[index]<<","<<(unsigned char)_qba.data()[index+1]<<")";
    18. uin=(unsigned int)(_qba.data()[index]*256+_qba.data()[index+1]);
    19. index+=2;
    20. uin &= 0x0000ffff;
    21. qDebug()<<uin;
    22. qDebug()<<"{"<<hi*256+lo<<"}";
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

    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!
    Last edited by wysota; 14th June 2010 at 10:10. Reason: changed [qtclass] to [code]

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: strange conversion!

    The operation:
    Qt Code:
    1. lo=(unsigned char)(u32r % 256);
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. //This code will only disect 16 bit values, for 32 values you will have to change the code in a similar way
    2. for(unsigned short i=0; i<dock->tableWidget->rowCount(); i++){
    3. unsigned short u16r=dock->tableWidget->item(i,2)->text().toUInt(&br);
    4. qDebug()<<"["<<u16r<<"]";
    5. if(!br)return;
    6. hi=(unsigned char)(u16r>>8);
    7. lo=(unsigned char)(u16r);
    8. ...
    9. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by high_flyer; 14th June 2010 at 10:37.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Oct 2009
    Posts
    90
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: strange conversion!

    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?
    Last edited by navid; 14th June 2010 at 11:03.

  4. #4
    Join Date
    Oct 2009
    Posts
    90
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: strange conversion!

    also, I have tested with bits shift operators, unfortunatly the result is the same
    >>8 for / 256
    <<8 for * 256
    Last edited by navid; 14th June 2010 at 10:53.

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: strange conversion!

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Oct 2009
    Posts
    90
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: strange conversion!

    Qt Code:
    1. uin=(((unsigned int)_qba.data()[index])<<8)+(unsigned int)(_qba.data()[index+1]);
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Oct 2009
    Posts
    90
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: strange conversion!

    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;

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: strange conversion!

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Hex Conversion
    By umulingu in forum Newbie
    Replies: 5
    Last Post: 5th March 2010, 18:07
  2. difficulty in conversion
    By mohanakrishnan in forum Qt Programming
    Replies: 3
    Last Post: 7th December 2009, 10:31
  3. Pixmap conversion
    By MrShahi in forum Qt Programming
    Replies: 7
    Last Post: 26th June 2008, 10:03
  4. Reg - Conversion of Qt3 to Qt4
    By suresh in forum Qt Programming
    Replies: 10
    Last Post: 28th August 2006, 23:10
  5. conversion of color
    By Stephano in forum Qt Programming
    Replies: 5
    Last Post: 22nd May 2006, 11:56

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.