Results 1 to 14 of 14

Thread: QByteArray does not process 0x80

  1. #1
    Join Date
    Oct 2009
    Location
    chennai,india
    Posts
    66
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default QByteArray does not process 0x80

    hi
    im using qbytearray to store hexadecimals and process it.
    Qt Code:
    1. ba.resize(1);
    2. ba[0] = 0x7e;
    3. if(ba.at(0)==0x7e)
    4. {
    5. //write the data to serial port
    6. }
    To copy to clipboard, switch view to plain text mode 
    the above code works
    my problem is if i use the hex value as 80 (ie greater t han integer 127)
    im unable to process the condition like below
    Qt Code:
    1. ba.resize(1);
    2. ba[0] = 0x80;
    3. if(ba.at(0)==0x80)
    4. {
    5. //write the data to serial port
    6. }
    To copy to clipboard, switch view to plain text mode 
    it does not work ,is there any settings?? to be done??
    or it accepts from -127 to 127
    can anyone help me???
    thanks

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QByteArray does not process 0x80

    You are comparing signed and unsigned values. Cast one of them to a signed value, or the other to a unsigned value.

  3. #3
    Join Date
    Oct 2009
    Location
    chennai,india
    Posts
    66
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QByteArray does not process 0x80

    hi
    thank for ur help pls tell me how to cast qbytearray to unsigned ??
    can u tell me??

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QByteArray does not process 0x80

    Look what QByteArray::at() returns - a char. If you want to compare it against 0x80 it should probably be unsigned char or an int, so cast it to one of the types as already suggested. Your compiler should have told you that, didn't it?

    main.cpp:9: warning: comparison is always false due to limited range of data type
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2009
    Location
    chennai,india
    Posts
    66
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QByteArray does not process 0x80

    hi wysota
    it is working but when in need to check each and every byte of the qbytearray then
    i need to call the conversion function those many times!!
    and my one more question is ,,qbytearray can only store -127 to 127??
    and not from 0 to 127!!
    pls tell me ...
    thanks

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QByteArray does not process 0x80

    Quote Originally Posted by mohanakrishnan View Post
    it is working but when in need to check each and every byte of the qbytearray then
    i need to call the conversion function those many times!!
    If you don't call QByteArray::at() then you won't have to perform the conversion.

    and my one more question is ,,qbytearray can only store -127 to 127??
    and not from 0 to 127!!
    It has nothing to do with QByteArray. char is your limitation here.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QByteArray does not process 0x80

    Quote Originally Posted by mohanakrishnan View Post
    and my one more question is ,,qbytearray can only store -127 to 127??
    and not from 0 to 127!!
    pls tell me ...
    thanks
    qbytearray stores standard signed 8-bit values, so that would be -128 to +127. It's not -127, as there's no -0 value, where as there is a +0, so + can only go upto 127.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QByteArray does not process 0x80

    Byte array stores bytes. Bytes in modern architectures have 8 bits. 8 bits allow us to store 256 different values. Therefore QByteArray can store values from 0 to 255 or from -255 to 0 or from -128 to 127 or from -64 to 191 or <provide your favourite two numbers that differ by 255>. It's all just a matter of interpreting the values which is exactly what casting to specific types does - it interprets values.

    Another fact is that QByteArray is meant to store character strings, that's why char is its natural mapping type. If you want to store eight bit integers, you can use QList<quint8> for that. This way you won't have problems with mapping to the interpretation of numbers most obvious for humans - natural numbers. quint8 is a typedef to unsigned char by the way...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    tanderson (9th December 2009)

  10. #9
    Join Date
    Oct 2009
    Location
    chennai,india
    Posts
    66
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QByteArray does not process 0x80

    hi
    thanks for ur suggestion .i used qlist<quint8> which is sucessful .....
    thanks..

  11. #10
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QByteArray does not process 0x80

    Quote Originally Posted by mohanakrishnan View Post
    Qt Code:
    1. ba.resize(1);
    2. ba[0] = 0x80;
    3. if(ba.at(0)==0x80)
    4. {
    5. //write the data to serial port
    6. }
    To copy to clipboard, switch view to plain text mode 
    Use '\x80' instead of 0x80 in the comparison.

  12. #11
    Join Date
    Oct 2009
    Location
    chennai,india
    Posts
    66
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Talking Re: QByteArray does not process 0x80

    hi coises,
    it is amazing ,it works well ,im trying to check this for the past one week..
    now i adopted using qlist <quint8> and char datatypes.but i ll use this also..
    thanks a lot

  13. #12
    Join Date
    Aug 2008
    Posts
    18
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QByteArray does not process 0x80

    Quote Originally Posted by wysota View Post
    Byte array stores bytes. Bytes in modern architectures have 8 bits. 8 bits allow us to store 256 different values. Therefore QByteArray can store values from 0 to 255 or from -255 to 0 or from -128 to 127 or from -64 to 191 or <provide your favourite two numbers that differ by 255>. It's all just a matter of interpreting the values which is exactly what casting to specific types does - it interprets values.

    Another fact is that QByteArray is meant to store character strings, that's why char is its natural mapping type. If you want to store eight bit integers, you can use QList<quint8> for that. This way you won't have problems with mapping to the interpretation of numbers most obvious for humans - natural numbers. quint8 is a typedef to unsigned char by the way...
    I think it is a design flaw in the Qt itself. Whenever involves an I/O operation, the byte type should be quint8 or unsigned char, not the char. Because the unsigned char covers the char very well. That is why everybody else defines their I/O buffer as void* or unsigned char*. Only Qt uses char*! (look at the QIODevice, QFile, QByteArray). When you deal with data in COM port, Bluetooth, FTP, or image files, you have to cast the raw bytes to chars first. Even their "RawData()" takes a char* type. Yes, it is doable, but it is confusing. In my limited knowledge about computer, I really do not know why they ever choose to design like that.

  14. #13
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QByteArray does not process 0x80

    To be honest 'char' doesn't specify the type. Most compiler will treat 'char', 'signed char' and 'unsigned char' as three completely different types, and you can normally tell the compiler which of the last two you prefer when you specify the former.

    In the automotive industry, if you use the type 'char' you can get cautioned by the code review board because of the above. You have to always prefix it with 'signed' or 'unsigned' to state exactly which you meant (or use an appropriate typedef).

    QByteArray uses char because it's really meant for text, and is also used by classes that process text (eg. QString). If you want a string of unsigned char's, then the class is of lesser use to you, so a more practical way would be QList<quint8>.

  15. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QByteArray does not process 0x80

    Quote Originally Posted by yren View Post
    Because the unsigned char covers the char very well.
    Really?

    Explain this then:
    Qt Code:
    1. #include <QtDebug>
    2.  
    3. int main(){
    4. char c;
    5. unsigned char uc;
    6. c = -14;
    7. uc = -14;
    8. qDebug() << (int)c << (int)uc;
    9. return 0;
    10. }
    To copy to clipboard, switch view to plain text mode 

    With the result of:
    $ ./c
    -14 242
    Could you remind me how to store -14 in unsigned char? Maybe I'm just doing it all wrong...

    That is why everybody else defines their I/O buffer as void*
    In C, not C++. Using void* in C++ is an error as void doesn't implicitly cast to/from any type in C++ as it did in C. And eventually you will have to make a cast to something and if so, why use void* in the first place?

    Only Qt uses char*! (look at the QIODevice, QFile, QByteArray).
    And this is bad, because...?

    When you deal with data in COM port, Bluetooth, FTP, or image files, you have to cast the raw bytes to chars first.
    You mean when using void*? And that's a good thing, because.... ?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. CRC 16 0xA001 polynomial
    By pdoria in forum Qt Programming
    Replies: 7
    Last Post: 28th March 2013, 17:42
  2. How to communicate Qt Process with non-qt process
    By nrabara in forum Qt for Embedded and Mobile
    Replies: 9
    Last Post: 15th February 2009, 21:01
  3. Process Read/Write
    By QbelcorT in forum Newbie
    Replies: 0
    Last Post: 20th November 2008, 03:08
  4. Send a key to process
    By Nyphel in forum Qt Programming
    Replies: 2
    Last Post: 9th July 2007, 17:37
  5. QDomElement to QByteArray ?
    By probine in forum Qt Programming
    Replies: 3
    Last Post: 2nd May 2006, 17:01

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.