Find 'unsigned char' in QByteArray similar to memchar()
-
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());@
-
"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()
-
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 ?
-
What about the operator[]. It will return a QByteRef, so a reference to the byte that was called.
-
Still no good. QByteRef is a wrapper for char&
-
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 valueSection 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
-
Unless I miss something, that use case is handled by QByteArray::indexOf()