Results 1 to 6 of 6

Thread: Convert 2 bytes of raw data to qint16

Hybrid View

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

    Default Re: Convert 2 bytes of raw data to qint16

    It never was hexadecimal to start with. You can use QString::number() as in my example, or QByteArray::toHex(), to display the value as a hexadecimal string.

    To transfer the two bytes into a QByteArray in a little-endian byte ordering you could also use:
    Qt Code:
    1. // No casting
    2. qint16 value = -443; // OR quint16 value = 65093;
    3. out.append(value & 0xFF);
    4. out.append((value >> 8) & 0xFF);
    5. qDebug() << out.toHex();
    6.  
    7. // OR
    8. out.resize(sizeof(value)); // Must have the space already allocated
    9. qToLittleEndian(value, reinterpret_cast<uchar *>(out.data()));
    10. qDebug() << out.toHex();
    To copy to clipboard, switch view to plain text mode 

    You could do the original job without casting also:
    Qt Code:
    1. QByteArray in("\x45\xfe", 2);
    2. qint16 value = in.at(1) << 8 | in.at(0);
    3. qDebug() << value << QString::number(value, 16);
    To copy to clipboard, switch view to plain text mode 

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

    Smosia (25th March 2014)

  3. #2
    Join Date
    Mar 2014
    Posts
    7
    Qt products
    Qt5
    Platforms
    MacOS X
    Thanks
    3

    Default Re: Convert 2 bytes of raw data to qint16

    Thank you all. That was really helpful. Topic is closed

Similar Threads

  1. Store data in bits & Bytes
    By 2lights in forum Newbie
    Replies: 3
    Last Post: 9th September 2013, 13:39
  2. Replies: 4
    Last Post: 23rd October 2012, 08:40
  3. Replies: 7
    Last Post: 12th September 2011, 10:52
  4. Convert int to Hex with fixed bytes
    By metRo_ in forum Qt Programming
    Replies: 3
    Last Post: 7th September 2011, 16:31
  5. How to convert binary data to hexadecimal data
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 8th March 2006, 16:17

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.