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. Find 'unsigned char' in QByteArray similar to memchar()
Forum Updated to NodeBB v4.3 + New Features

Find 'unsigned char' in QByteArray similar to memchar()

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 2.7k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    SeedOfLife
    wrote on last edited by
    #1

    Is there a way to search for an unsigned char character in a QByteArray object ? Are there any specific methods ?

    Right now I rely on memchar() and pass the QByteArray object's underlying char array to it.

    @char* pFoundEOT = (char*)memchr(o_qbInputBuffer.data(), (const char)0x04, o_qbInputBuffer.length());@

    1 Reply Last reply
    0
    • jeremy_kJ Online
      jeremy_kJ Online
      jeremy_k
      wrote on last edited by
      #2

      "QByteArray::indexOf(char ch, int from = 0)":http://qt-project.org/doc/qt-5/qbytearray.html#indexOf-4

      The char to look for is signed, but so are the members of the array returned by QByteArray::data()

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SeedOfLife
        wrote on last edited by
        #3

        Ok i used that and got it working. I think I can live with passing signed chars.

        But the docs say: "QByteArray can be used to store both raw bytes (including '\0's) and traditional 8-bit '\0'-terminated strings"

        Going by this (saving any type of byte) if we provide unsigned char to the QByteArray object how can we retrieve it ?

        1 Reply Last reply
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          What about the operator[]. It will return a QByteRef, so a reference to the byte that was called.

          Greetz, Jeroen

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SeedOfLife
            wrote on last edited by
            #5

            Still no good. QByteRef is a wrapper for char&

            1 Reply Last reply
            0
            • jeremy_kJ Online
              jeremy_kJ Online
              jeremy_k
              wrote on last edited by
              #6

              operator[] doesn't help find a byte with a particular value in the array.

              The C++ 0x11 (draft) standard says don't worry.

              Section 3.9 point 2:

              For any object (other than a base-class subobject) of trivially copyable type T, [...] the underlying bytes [...] can be copied into an array of char or unsigned char.
              If the content of the array of char or unsigned char is copied back into the object, the object shall subsequently hold its original value

              Section 3.9.1 point 3

              For each of the standard signed integer types, there exists a corresponding (but different) standard un- signed integer type: “unsigned char”, [...], each of which occupies the same amount of storage and has the same alignment requirements

              So copying a unsigned char into a signed char isn't going to destroy it. Copying it back out will result in the same value. If copying the same value in results in two different values, I'm not sure what to think...

              The draft is available at http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf if you're interested in the subject

              Asking a question about code? http://eel.is/iso-c++/testcase/

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andrep
                wrote on last edited by
                #7

                Unless I miss something, that use case is handled by QByteArray::indexOf()

                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