Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    QByteArray comparator

    General and Desktop
    4
    8
    5803
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      phamtv last edited by

      I am working on application and ran across and issue I need some confirmation on. I have a byte array of data received from a device that can have values ranging from 0x00 - 0xFF. In my program, I have a statement as follow:

      @
      QByteArray rcvd_buffer; // received data read from device

      if (rcvd_buffer[i] > 0xC8)
      {
      ....
      }
      @
      The above logic is always true but my rcvd_buffer[i] value is a 5.
      Why is this the case? Do I have to cast rcvd_buffer[i] as a byte to have it worked properly?

      1 Reply Last reply Reply Quote 0
      • D
        danilocesar last edited by

        well. gcc complains about it, but I don't know why yet..
        I'm doing some test.

        <a href="http://www.danilocesar.com">Danilo Cesar Lemes de Paula</a>
        Software Engineer

        1 Reply Last reply Reply Quote 0
        • P
          phamtv last edited by

          just a thought... do you think it is because rcvd_buffer[i] is a char and characters can only be from 0-0x7F?

          1 Reply Last reply Reply Quote 0
          • D
            danilocesar last edited by

            Certainly is something related to the conversion.
            I'm still looking for it..

            Someone else?

            <a href="http://www.danilocesar.com">Danilo Cesar Lemes de Paula</a>
            Software Engineer

            1 Reply Last reply Reply Quote 0
            • D
              danilocesar last edited by

              outch...

              0xC8 is negative if you compare it with a char...

              cast it to unsigned char and it will work:
              @if ((unsigned char) ba[1] > 0xC8)@

              <a href="http://www.danilocesar.com">Danilo Cesar Lemes de Paula</a>
              Software Engineer

              1 Reply Last reply Reply Quote 0
              • P
                phamtv last edited by

                it looks like I have to cast my data to (quint8)rcvd_buffer[i] since my data can range from 0x00-0xFF... I thought that QByteArray should be an array of unsigned char instead of signed char.... I assume I will have to double check to see if me assigning negative values to rcvd_buffer[i] would error out as well... Please post if anyone has any insights...

                1 Reply Last reply Reply Quote 0
                • A
                  Asazeus last edited by

                  I use this method of comparation QByteArray element with literal in Qt5.2.1:
                  @if ( ans[startIndex+1].operator ==(0xFF) ) {/.../}
                  @

                  it is wierd, but it works :)

                  1 Reply Last reply Reply Quote 0
                  • Chris Kawa
                    Chris Kawa Moderators last edited by

                    Asazeus - you do realize that this is the same as
                    @ if(ans[startIndex+1] == 0xFF)
                    @
                    just uglier and less readable? There's almost never a reason to call operators explicitly.

                    The problem is that == will work, but < or > will not in this case. It's a signed/unsigned problem as noted before.

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post