Results 1 to 15 of 15

Thread: Bitwise shifting and ORing HexaDecimal Value

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Convert QByteArray to quint 16

    You kept saying you wanted to obtain a set of 16 bit integers. You can convert these to a hex string int by int: there is no toHex() method in QList, and there is no QByteArray involved.

    There are a lot of ways to do things vaguely like your request but you have to actually understand what you are trying to achieve and know some basic programming.

    Qt Code:
    1. QList<quint16> result; // A list of 16 bit unsigned integers
    2. for (int i = 0; i < ba.size(); i+=2)
    3. result.append( (static_cast<unsigned char>(ba.at(i) << 5)) | static_cast<unsigned char>(ba.at(i+1)) );
    4. // You now have a list of 16 bit ints like you requested.
    5.  
    6. // If you wanted only a hex string
    7. QString hex;
    8. for (int i = 0; i < ba.size(); i+=2) {
    9. quint16 r = static_cast<unsigned char>(ba.at(i) << 5)) | static_cast<unsigned char>(ba.at(i+1);
    10. result += QString(%1").arg(r, 4, 16, '0' ); // watch out for byte order if this needs to be portable
    11. }
    To copy to clipboard, switch view to plain text mode 

    Given that you already have the data in a buffer, and the result will be exactly the same size as the source data, then you could have done the job in-situ with a bit of thought:
    Qt Code:
    1. // char *buff
    2. // int numbytes
    3. for (int i = 0; i < numbytes; i+=2) {
    4. unsigned char a = static_cast<unsigned char>( buff[i] );
    5. unsigned char b = static_cast<unsigned char>( buff[i+1] );
    6. buff[i] = a >> 3;
    7. buff[i+1] = a << 5 | b;
    8. }
    To copy to clipboard, switch view to plain text mode 

  2. The following user says thank you to ChrisW67 for this useful post:

    nagabathula (27th November 2010)

Similar Threads

  1. Shifting a QImage up by 1 pixel row
    By MSUdom5 in forum Qt Programming
    Replies: 2
    Last Post: 7th May 2010, 10:25
  2. LineEdit for Hexadecimal input
    By mastupristi in forum Qt Programming
    Replies: 2
    Last Post: 21st January 2010, 14:51
  3. how to process hexadecimal
    By mohanakrishnan in forum Qt Programming
    Replies: 2
    Last Post: 20th November 2009, 04:33
  4. Byte shifting in C
    By tntcoda in forum General Programming
    Replies: 3
    Last Post: 14th November 2008, 22:40
  5. Replies: 2
    Last Post: 4th August 2008, 08:14

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.