Results 1 to 17 of 17

Thread: Qbytearray easy question

  1. #1

    Default Qbytearray easy question

    Hi

    I have the following problem:

    Qt Code:
    1. test[0]= 0xAB;
    2. test[1]= 0xCD;
    3. test[2]= 0xEF;
    4. test[3]= 0x01;
    5. test[4]= 0x02;
    6. test[5]= 0x03;
    7. test[6]= 0x04;
    8.  
    9. qDebug () << test.at(0);
    To copy to clipboard, switch view to plain text mode 

    test.at(0) gives me QChar but I need to display Hex Value

    for at(0) is diplayed « instead of 0xAB

    How to display hex value ?

    Regards
    Artur

  2. #2
    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: Qbytearray easy question

    QByteArray::toHex() to convert the whole array.
    QByteArray::number(value, 16) to do a single byte (Or QString::number(), QString::arg() etc)

  3. #3

    Default Re: Qbytearray easy question

    Thank you for help

    It works.

    qDebug() << QByteArray::number(test.at(0),16);

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Qbytearray easy question

    QDebug supports QTextStream's manipulators, so use the hex manipulator and maybe you need to cast the char to an int:
    Qt Code:
    1. qDebug() << hex << (int)test.at(0);
    To copy to clipboard, switch view to plain text mode 

  5. #5

    Default Re: Qbytearray easy question

    something is still wrong ( I make a mistake)

    In my case

    Qt Code:
    1. test[0]= 0xAB;
    2. test[1]= 0xCD;
    3. test[2]= 0xEF;
    4. test[3]= 0x01;
    5. test[4]= 0x02;
    6. test[5]= 0x03;
    7. test[6]= 0x04;
    8.  
    9. for (int m=0;m<7;m++) qDebug() << QByteArray::number(test.at(m),16);
    To copy to clipboard, switch view to plain text mode 

    The result is :

    "ffffffab"
    "ffffffcd"
    "ffffffef"
    "1"
    "2"
    "3"
    "4"

    I do not know why. It would be great to display "01" instead of "1".

  6. #6
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Qbytearray easy question

    Note that QByteArray is using a signed char* buffer, so you get a truncation for 0xAB and so on (max value is 127, hex 0x7F).

    As for your second question, use QString and arg:
    Qt Code:
    1. for (int m=0;m<7;m++)
    2. qDebug() << hex << QString("%0").arg(test.at(m), 2, 16, QChar('0'));
    To copy to clipboard, switch view to plain text mode 

  7. #7

    Default Re: Qbytearray easy question

    How to convert signed byte to unsigned? I receive information from serial port and it is unsigned information.

    Regards
    Artur

  8. #8
    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: Qbytearray easy question

    The each signed char (a byte) in the array can hold a signed decimal value between -128 and 127. The hex representation of the bits in the byte has no sign, so:
    Qt Code:
    1. QByteArray b = "\x7f\xab\xff";
    2. qDebug() << b.toHex();
    To copy to clipboard, switch view to plain text mode 
    Prints "7fabff" as you expect given what we put in the array
    Qt Code:
    1. // first the signed interpretation of each byte
    2. foreach (char sc, b) {
    3. qDebug() << QByteArray::number(sc) << QByteArray::number(sc, 16);
    4. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. "127" "7f"
    2. "-85" "ffffffffffffffab"
    3. "-1" "ffffffffffffffff"
    To copy to clipboard, switch view to plain text mode 
    The extended hex of the negative numbers is the result of sign extension to a 64-bit int as argument to number().
    Qt Code:
    1. // and the unsigned version
    2. foreach (char sc, b) {
    3. unsigned char uc = static_cast<unsigned char>(sc);
    4. qDebug() << QByteArray::number(uc) << QByteArray::number(uc, 16);
    5. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. "127" "7f"
    2. "171" "ab"
    3. "255" "ff"
    To copy to clipboard, switch view to plain text mode 
    You see the same bit pattern being interpreted in two different ways through the static_cast.

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

    Default Re: Qbytearray easy question

    QByteArray::at() or QByteArray[] is a char - not a QChar. The QChar is a result of a conversion made by qDebug(). If you know the size of the QByteArray, then
    Qt Code:
    1. const unsigned char *pdata = reinterpret_cast<const unsigned char *>(thearray.constData());
    To copy to clipboard, switch view to plain text mode 
    gives you a pointer to binary data in the QByteArray. The casting is necessary to get rid of signed values (see above, where 0xA0 was output as 0xFFA0, etc.) Now, you can, for example:
    Qt Code:
    1. qDebug << pdata[i];
    To copy to clipboard, switch view to plain text mode 
    or convert to int by simple assigning:
    Qt Code:
    1. int val = pdata[i]; // no sign extension
    To copy to clipboard, switch view to plain text mode 

  10. #10

    Default Re: Qbytearray easy question

    Could you write me proper code for my code

    Qt Code:
    1. QByteArray test ;
    2. test[0] = 0xFF;
    3. unsigned char ch = 0;
    4.  
    5. ch = 0xff;
    6. if (test.at(0)==ch) qDebug () << "OK";
    7.  
    8. qDebug () << ch;
    9.  
    10. qDebug () << test.at(0);
    To copy to clipboard, switch view to plain text mode 

    because test[0] is signed and ch is unsigned the condition is false. I will try to use your method but without success.

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

    Default Re: Qbytearray easy question

    Try
    Qt Code:
    1. if( (unsigned char)(test.at(0)) == ch ) qDebug() << "OK";
    To copy to clipboard, switch view to plain text mode 

  12. #12

    Default Re: Qbytearray easy question

    It works perfectly :

    Thank you for help

  13. #13

    Default Re: Qbytearray easy question

    I have additional qestion

    Qt Code:
    1. test[0]= 0xAB;
    2. test[1]= 0xCD;
    3. test[2]= 0xEF;
    4. test[3]= 0x01;
    5. test[4]= 0x02;
    6. test[5]= 0x03;
    7. test[6]= 0x04;
    To copy to clipboard, switch view to plain text mode 

    I need to take only the following bytes
    test[1]= 0xCD;
    test[2]= 0xEF;
    test[4]= 0x02;
    test[5]= 0x03;

    and show it as a single number

    0xCDEF0203 = 3454992899

    I try to use the code but it does not work

    Qt Code:
    1. usnigned long num;
    2. num |= (unsigned char)(test[1]) << 8;
    3. num |= (unsigned char)(test[2]) << 8;
    4. num |= (unsigned char)(test[4]) << 8;
    5. num |= (unsigned char)(test[5]) << 8 ;
    To copy to clipboard, switch view to plain text mode 

    Why it does not work ? Maybe is it easier method to do it ?

    Regards
    Artur

  14. #14
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qbytearray easy question

    First, in your code the variable num is not initialized before you apply operator |= to it. Second, you shift each of the test[] values by 8 bit and combine the shifted value into num. What you need to do is: initialize num to test[1]. Before adding test[2], shift num by 8 bit and then add test[2]. Same procedure for test[4] and test[5]. It may look like
    Qt Code:
    1. unsigned long num = (unsigned char)(test[1]) ;
    2. num = num << 8; num |= (unsigned char)(test[2]) ;
    3. num = num << 8; num |= (unsigned char)(test[4]) ;
    4. num = num << 8; num |= (unsigned char)(test[5]) ;
    To copy to clipboard, switch view to plain text mode 

  15. #15

    Default Re: Qbytearray easy question

    Yes. It works.

    I have last question.

    How to convert my QbyteArray test to QString ?

    Regards
    Artur

  16. #16
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qbytearray easy question

    See the constructors for QString (http://doc.qt.io/qt-4.8/qstring.html). You can do
    Qt Code:
    1. QString s(test);
    To copy to clipboard, switch view to plain text mode 
    to initalize QString s from QByteArray test. From the documentation:

    QString::QString(const QByteArray & ba)

    Constructs a string initialized with the byte array ba. The given byte array is converted to Unicode using fromAscii(). Stops copying at the first 0 character, otherwise copies the entire byte array.

    You can disable this constructor by defining QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example.

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

    Default Re: Qbytearray easy question

    Please check the Qt classes reference before posting on the forum.

    The wanted QString is produced by "fromXXX" static members of QString. If the QByteArray contains UTF8 data (as a byte array) then
    Qt Code:
    1. QString test2str = QString::fromUtf8(test);
    To copy to clipboard, switch view to plain text mode 
    There are more "fromXXX" functions defined in QString.

Similar Threads

  1. Replies: 6
    Last Post: 14th May 2014, 12:14
  2. SVG to QByteArray
    By meazza in forum Newbie
    Replies: 4
    Last Post: 5th July 2011, 23:05
  3. Replies: 1
    Last Post: 22nd June 2011, 08:12
  4. Replies: 9
    Last Post: 25th July 2009, 13:27
  5. QByteArray in Qt3
    By joseph in forum Qt Programming
    Replies: 1
    Last Post: 6th September 2007, 06:16

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.