Results 1 to 17 of 17

Thread: Qbytearray easy question

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    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.

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

    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 

  3. #3

    Default Re: Qbytearray easy question

    It works perfectly :

    Thank you for help

  4. #4

    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

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

    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 

  6. #6

    Default Re: Qbytearray easy question

    Yes. It works.

    I have last question.

    How to convert my QbyteArray test to QString ?

    Regards
    Artur

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

    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.

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

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