Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QByteArray comparator
Forum Update on Monday, May 27th 2025

QByteArray comparator

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 6.3k Views
  • 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 Offline
    P Offline
    phamtv
    wrote on last edited by
    #1

    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
    0
    • D Offline
      D Offline
      danilocesar
      wrote on last edited by
      #2

      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
      0
      • P Offline
        P Offline
        phamtv
        wrote on last edited by
        #3

        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
        0
        • D Offline
          D Offline
          danilocesar
          wrote on last edited by
          #4

          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
          0
          • D Offline
            D Offline
            danilocesar
            wrote on last edited by
            #5

            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
            0
            • P Offline
              P Offline
              phamtv
              wrote on last edited by
              #6

              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
              0
              • A Offline
                A Offline
                Asazeus
                wrote on last edited by
                #7

                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
                0
                • Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved