Results 1 to 6 of 6

Thread: Convert 2 bytes of raw data to qint16

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

    Default Convert 2 bytes of raw data to qint16

    Hello.
    Can you help me. I tried many methods to do that, but i didn't find solution.
    I have 2 bytes of raw data (like that "45 FE"), stored in QByteArray (char 45, char FE). I want to convert them to qint16 (to decimal).
    I have working solution (it's crazy ).
    Qt Code:
    1. QByteArray buffer1;
    2. qFromLittleEndian<qint16>((qint16)(buffer1.mid(i,1).at(0)))*256 + qFromLittleEndian<quint16>((const uchar *)buffer1.mid(i+1,1).constData());
    To copy to clipboard, switch view to plain text mode 
    Algoritm:
    For numers > 0
    converted first byte to signed qint16 * 256 + converted second byte to unsigned quint16.
    For numbers < 0:
    converted first byte to signed qint16 * 256 - converted second byte to unsigned quint16

    256 = 100 in hex.

    Could you recommend me right algorithm i think my algorithm is quite sophisticated

  2. #2
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Convert 2 bytes of raw data to qint16

    Qt Code:
    1. quint16 res = *reinterpret_cast<const quint16 *>(&buffer1.constData[i]); // if you want 0xFE45
    2.  
    3. res = ((res << 8) | (res >> 8)); // if you want 0x45FE
    To copy to clipboard, switch view to plain text mode 
    Last edited by Radek; 23rd March 2014 at 19:28.

  3. The following user says thank you to Radek for this useful post:

    Smosia (23rd March 2014)

  4. #3
    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 2 bytes of raw data to qint16

    Slightly different approach. Different result if you use quint16 versus qint16, your initial post was unclear which you wanted:
    Qt Code:
    1. QByteArray in("\x45\xfe", 2);
    2. qint16 value1 = qFromLittleEndian<qint16>(reinterpret_cast<const unsigned char*>(in.constData()));
    3. qDebug() << value1 << QString::number(value1, 16);
    4. // -443 "fffffffffffffe45"
    5. // ^^ The number is negative and has been sign extended to the size of a long.
    6.  
    7. quint16 value2 = qFromLittleEndian<quint16>(reinterpret_cast<const unsigned char*>(in.constData()));
    8. qDebug() << value2 << QString::number(value2, 16);
    9. // 65093 "fe45"
    To copy to clipboard, switch view to plain text mode 

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

    Smosia (25th March 2014)

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

    Default Re: Convert 2 bytes of raw data to qint16

    Thank you, that was useful for me.
    And if I will need to convert qint16 back to hex and add it back to QbyteArray. I can do something like that?
    Qt Code:
    1. QByteArray buffer1;
    2. qint16 buff = -443;
    3. char * byte_buff= reinterpret_cast<char *>(&buff);
    4. buffer1.append(*(byte_buff+1));
    5. buffer1.append(*byte_buff);
    To copy to clipboard, switch view to plain text mode 

  7. #5
    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 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 

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

    Smosia (25th March 2014)

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

    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.