QKnxByteArray Class

The QKnxByteArray class provides an array of unsigned bytes. More...

Header: #include <QKnxByteArray>
qmake: QT += knx

Public Types

Public Functions

QKnxByteArray(QKnxByteArray &&other)
QKnxByteArray(const QKnxByteArray &other)
QKnxByteArray(std::initializer_list<quint8> args)
QKnxByteArray(const quint8 *data, int size)
QKnxByteArray(const char *data, int size)
QKnxByteArray(int size, quint8 ch)
QKnxByteArray()
QKnxByteArray &operator=(QKnxByteArray &&other)
QKnxByteArray &operator=(const QKnxByteArray &other)
~QKnxByteArray()
QKnxByteArray &append(const QKnxByteArray &ba)
QKnxByteArray &append(quint8 ch)
QKnxByteArray &append(int count, quint8 ch)
quint8 at(int i) const
QKnxByteArray::iterator begin()
QKnxByteArray::const_iterator begin() const
QKnxByteArray::const_iterator cbegin() const
QKnxByteArray::const_iterator cend() const
QKnxByteArray chop(int len) const
void clear()
QKnxByteArray::const_iterator constBegin() const
const quint8 *constData() const
QKnxByteArray::const_iterator constEnd() const
bool contains(const QKnxByteArray &ba) const
bool contains(quint8 ch) const
QKnxByteArray::const_reverse_iterator crbegin() const
QKnxByteArray::const_reverse_iterator crend() const
quint8 *data()
const quint8 *data() const
QKnxByteArray::iterator end()
QKnxByteArray::const_iterator end() const
bool endsWith(const QKnxByteArray &ba) const
bool endsWith(quint8 ch) const
QKnxByteArray &fill(quint8 ch, int size = -1)
int indexOf(const QKnxByteArray &ba, int from = 0) const
int indexOf(quint8 ch, int from = 0) const
QKnxByteArray &insert(int i, const QKnxByteArray &ba)
QKnxByteArray &insert(int i, quint8 ch)
QKnxByteArray &insert(int i, int count, quint8 ch)
bool isEmpty() const
bool isNull() const
int lastIndexOf(const QKnxByteArray &ba, int from = -1) const
int lastIndexOf(quint8 ch, int from = -1) const
QKnxByteArray left(int len) const
QKnxByteArray mid(int pos, int len = -1) const
QKnxByteArray &prepend(const QKnxByteArray &ba)
QKnxByteArray &prepend(quint8 ch)
QKnxByteArray &prepend(int count, quint8 ch)
QKnxByteArray::reverse_iterator rbegin()
QKnxByteArray::const_reverse_iterator rbegin() const
QKnxByteArray &remove(int pos, int len)
QKnxByteArray::reverse_iterator rend()
QKnxByteArray::const_reverse_iterator rend() const
QKnxByteArray repeated(int times) const
QKnxByteArray &replace(int index, int len, const QKnxByteArray &after)
QKnxByteArray &replace(quint8 before, const QKnxByteArray &after)
QKnxByteArray &replace(const QKnxByteArray &before, const QKnxByteArray &after)
QKnxByteArray &replace(quint8 before, quint8 after)
void resize(int size)
QKnxByteArray right(int len) const
void set(int i, quint8 val)
void setValue(int i, quint8 val)
int size() const
bool startsWith(const QKnxByteArray &ba) const
bool startsWith(quint8 ch) const
void swap(QKnxByteArray &other)
const QByteArray &toByteArray() const
QKnxByteArray toHex(quint8 separator = '\0') const
quint8 value(int i, quint8 defaultValue = {}) const
QKnxByteArray &operator+=(const QKnxByteArray &ba)
QKnxByteArray &operator+=(quint8 ch)

Static Public Members

QKnxByteArray fromByteArray(const QByteArray &byteArray)
QKnxByteArray fromHex(const QKnxByteArray &hexEncoded)
QKnxByteArray fromHex(const QByteArray &hexEncoded)
uint qHash(const QKnxByteArray &key, uint seed = 0)
bool operator!=(const QKnxByteArray &a1, const QKnxByteArray &a2)
const QKnxByteArray operator+(const QKnxByteArray &a1, const QKnxByteArray &a2)
const QKnxByteArray operator+(const QKnxByteArray &ba, quint8 ch)
const QKnxByteArray operator+(quint8 ch, const QKnxByteArray &ba)
QDebug operator<<(QDebug debug, const QKnxByteArray &byteArray)
bool operator==(const QKnxByteArray &a1, const QKnxByteArray &a2)

Detailed Description

A KNX byte array can be used to store both raw bytes (including the string \0 and any characters that might come after it) and traditional 8-bit \0-terminated strings. Using QKnxByteArray is much more convenient than using const quint8 *. Behind the scenes, it always ensures that the data is followed by a null-terminator (\0).

QKnxByteArray makes a deep copy of the data given, so you can modify it later without experiencing side effects.

Another approach is to set the size of the array using resize() and to initialize the data byte per byte. QKnxByteArray uses 0-based indexes, just like C++ arrays. To access the byte at a particular index position, you can use at(), set(), value() and setValue().

To extract several bytes at a time, use left(), right(), or mid().

A KNX byte array can embed \0 bytes. The size() function always returns the size of the whole array, including the embedded \0 bytes, but excluding the null-terminator added by QKnxByteArray.

After a call to resize(), newly allocated bytes have undefined values. To set all the bytes to a particular value, call fill().

To obtain a pointer to the actual character data, call data() or constData(). These functions return a pointer to the beginning of the data. The pointer is guaranteed to remain valid until a non-const function is called on the byte array.

Member Type Documentation

typedef QKnxByteArray::const_iterator

This typedef provides an STL-style const iterator for QKnxByteArray.

typedef QKnxByteArray::const_reverse_iterator

This typedef provides an STL-style const reverse iterator for QKnxByteArray.

typedef QKnxByteArray::iterator

This typedef provides an STL-style non-const iterator for QKnxByteArray.

typedef QKnxByteArray::reverse_iterator

This typedef provides an STL-style non-const reverse iterator for QKnxByteArray.

Member Function Documentation

QKnxByteArray::QKnxByteArray(QKnxByteArray &&other)

Move-constructs a QKnxByteArray instance, making it point to the same object that other was pointing to.

QKnxByteArray::QKnxByteArray(const QKnxByteArray &other)

Constructs a copy of other.

QKnxByteArray::QKnxByteArray(std::initializer_list<quint8> args)

Constructs a byte array from the std::initializer_list specified by args.

QKnxByteArray::QKnxByteArray(const quint8 *data, int size)

This function overloads QKnxByteArray().

QKnxByteArray::QKnxByteArray(const char *data, int size)

Constructs a byte array from data containing the number of bytes specified by size starting from the beginning of the data.

If data is 0, a null byte array is constructed.

If size is negative, data is assumed to point to a null-terminated string and its length is determined dynamically. The terminating null-character is not considered part of the byte array.

QKnxByteArray makes a deep copy of the string data.

QKnxByteArray::QKnxByteArray(int size, quint8 ch)

Constructs a byte array of the size size with every byte set to the character ch.

QKnxByteArray::QKnxByteArray()

Constructs an empty byte array.

QKnxByteArray &QKnxByteArray::operator=(QKnxByteArray &&other)

Move-assigns other to this QKnxByteArray instance.

QKnxByteArray &QKnxByteArray::operator=(const QKnxByteArray &other)

Assigns other to this KNX byte array and returns a reference.

QKnxByteArray::~QKnxByteArray()

Destroys the byte array.

QKnxByteArray &QKnxByteArray::append(const QKnxByteArray &ba)

Appends the byte array ba to the end of this byte array.

QKnxByteArray &QKnxByteArray::append(quint8 ch)

This function overloads append().

Appends the character ch to this byte array.

QKnxByteArray &QKnxByteArray::append(int count, quint8 ch)

This function overloads append().

Appends the number of copies of the character ch specified by count to this byte array and returns a reference to this byte array.

If count is negative or zero, nothing is appended to the byte array.

quint8 QKnxByteArray::at(int i) const

Returns the character at the index position i in the byte array.

i must be a valid index position in the byte array (that is, between 0 and the value returned by size()).

QKnxByteArray::iterator QKnxByteArray::begin()

Returns an STL-style iterator pointing to the first character in the byte array.

QKnxByteArray::const_iterator QKnxByteArray::begin() const

This function overloads begin().

QKnxByteArray::const_iterator QKnxByteArray::cbegin() const

Returns a const STL-style iterator pointing to the first character in the byte array.

QKnxByteArray::const_iterator QKnxByteArray::cend() const

Returns a const STL-style iterator pointing to the imaginary character after the last character in the list.

QKnxByteArray QKnxByteArray::chop(int len) const

Returns a byte array that contains the number of leftmost bytes in this byte array calculated by subtracting the amount of bytes specified by len from the amount of bytes of returned by size().

Note: The behavior is undefined if len is negative.

void QKnxByteArray::clear()

Clears the contents of the byte array and makes it null.

QKnxByteArray::const_iterator QKnxByteArray::constBegin() const

Returns a const STL-style iterator pointing to the first character in the byte array.

const quint8 *QKnxByteArray::constData() const

Returns a pointer to the data stored in the byte array. The pointer can be used to access the bytes that compose the array. The data is null-terminated.

The pointer remains valid until the byte array is reallocated or destroyed.

QKnxByteArray::const_iterator QKnxByteArray::constEnd() const

Returns a const STL-style iterator pointing to the imaginary character after the last character in the list.

bool QKnxByteArray::contains(const QKnxByteArray &ba) const

Returns true if the byte array contains an occurrence of the byte array ba; otherwise returns false.

bool QKnxByteArray::contains(quint8 ch) const

This function overloads contains().

Returns true if the byte array contains the character ch; otherwise returns false.

QKnxByteArray::const_reverse_iterator QKnxByteArray::crbegin() const

Returns a const STL-style reverse iterator pointing to the first character in the byte array, in reverse order.

QKnxByteArray::const_reverse_iterator QKnxByteArray::crend() const

Returns a const STL-style reverse iterator pointing to one past the last character in the byte array, in reverse order.

quint8 *QKnxByteArray::data()

Returns a pointer to the data stored in the byte array. The pointer can be used to access and modify the bytes that compose the array. The data is null-terminated, which means that the number of bytes in the returned character string is the value returned by size() plus 1 for the null-terminator.

The pointer remains valid until the byte array is reallocated or destroyed.

const quint8 *QKnxByteArray::data() const

This function overloads data().

QKnxByteArray::iterator QKnxByteArray::end()

Returns an STL-style iterator pointing to the imaginary character after the last character in the byte array.

QKnxByteArray::const_iterator QKnxByteArray::end() const

This function overloads end().

bool QKnxByteArray::endsWith(const QKnxByteArray &ba) const

Returns true if this byte array ends with the byte array ba; otherwise returns false.

bool QKnxByteArray::endsWith(quint8 ch) const

This function overloads endsWith().

Returns true if this byte array ends with the character ch; otherwise returns false.

QKnxByteArray &QKnxByteArray::fill(quint8 ch, int size = -1)

Sets every byte in the byte array to the character ch and returns a reference to this byte array. If size is different from -1, the byte array is resized to size beforehand.

[static] QKnxByteArray QKnxByteArray::fromByteArray(const QByteArray &byteArray)

Returns a byte array constructed from byteArray .

[static] QKnxByteArray QKnxByteArray::fromHex(const QKnxByteArray &hexEncoded)

Returns a decoded copy of the hex encoded array hexEncoded. Input is not checked for validity; invalid characters in the input are skipped, enabling the decoding process to continue with subsequent characters.

[static] QKnxByteArray QKnxByteArray::fromHex(const QByteArray &hexEncoded)

This function overloads fromHex().

int QKnxByteArray::indexOf(const QKnxByteArray &ba, int from = 0) const

Returns the index position of the first occurrence of the byte array ba in this byte array, searching forward from the index position from. Returns -1 if ba could not be found.

int QKnxByteArray::indexOf(quint8 ch, int from = 0) const

This function overloads indexOf().

Returns the index position of the first occurrence of the character ch in the byte array, searching forward from the index position from. Returns -1 if ch could not be found.

QKnxByteArray &QKnxByteArray::insert(int i, const QKnxByteArray &ba)

Inserts the byte array ba at the index position i and returns a reference to this byte array.

QKnxByteArray &QKnxByteArray::insert(int i, quint8 ch)

This function overloads insert().

Inserts the character ch at the index position i in the byte array. If i is greater than the number of bytes returned by size(), the array is first extended using resize().

QKnxByteArray &QKnxByteArray::insert(int i, int count, quint8 ch)

This function overloads insert().

Inserts the number of copies of the character ch specified by count at the index position i in the byte array.

If i is greater than the value returned by size(), the array is first extended using resize().

bool QKnxByteArray::isEmpty() const

Returns true if the byte array has the size 0; otherwise returns false.

bool QKnxByteArray::isNull() const

Returns true if this byte array is null; otherwise returns false.

int QKnxByteArray::lastIndexOf(const QKnxByteArray &ba, int from = -1) const

Returns the index position of the last occurrence of the byte array ba in this byte array, searching backward from the index position from. If from is -1 (the default), the search starts at the last byte. Returns -1 if ba could not be found.

int QKnxByteArray::lastIndexOf(quint8 ch, int from = -1) const

This function overloads lastIndexOf().

Returns the index position of the last occurrence of the character ch in the byte array, searching backward from the index position from. If from is -1 (the default), the search starts at the last byte (that is, the value returned by size() minus 1). Returns -1 if ch cannot be found.

QKnxByteArray QKnxByteArray::left(int len) const

Returns a byte array that contains the number of leftmost bytes in the byte array specified by len.

The entire byte array is returned if len is greater than the value returned by size().

QKnxByteArray QKnxByteArray::mid(int pos, int len = -1) const

Returns a byte array containing the number of bytes in the byte array specified by len, starting at the position pos.

If len is -1 (the default), or pos added to len is equal to or larger than the value returned by size(), returns a byte array containing all bytes starting from the position pos to the end of the byte array.

QKnxByteArray &QKnxByteArray::prepend(const QKnxByteArray &ba)

Prepends the byte array ba to this byte array and returns a reference to this byte array.

QKnxByteArray &QKnxByteArray::prepend(quint8 ch)

This function overloads prepend().

Prepends the character ch to this byte array.

QKnxByteArray &QKnxByteArray::prepend(int count, quint8 ch)

This function overloads prepend().

Prepends the number of copies of the character ch specified by count to this byte array.

QKnxByteArray::reverse_iterator QKnxByteArray::rbegin()

Returns a STL-style reverse iterator pointing to the first character in the byte array, in reverse order.

QKnxByteArray::const_reverse_iterator QKnxByteArray::rbegin() const

This function overloads rbegin().

QKnxByteArray &QKnxByteArray::remove(int pos, int len)

Removes the number of bytes specified by len from the array, starting at the index position pos, and returns a reference to the array.

If pos is out of range, nothing happens. If pos is valid, but pos added to len is larger than the size of the array, the array is truncated at the position pos.

QKnxByteArray::reverse_iterator QKnxByteArray::rend()

Returns a STL-style reverse iterator pointing to one past the last character in the byte array, in reverse order.

QKnxByteArray::const_reverse_iterator QKnxByteArray::rend() const

This function overloads rend().

QKnxByteArray QKnxByteArray::repeated(int times) const

Returns a copy of this byte array repeated the specified number of times.

If times is less than 1, an empty byte array is returned.

QKnxByteArray &QKnxByteArray::replace(int index, int len, const QKnxByteArray &after)

Replaces the number of bytes specified by len beginning at the index position index with the byte array after, and returns a reference to this byte array.

QKnxByteArray &QKnxByteArray::replace(quint8 before, const QKnxByteArray &after)

This function overloads replace().

Replaces every occurrence of the character before with the byte array after.

QKnxByteArray &QKnxByteArray::replace(const QKnxByteArray &before, const QKnxByteArray &after)

This function overloads replace().

Replaces every occurrence of the byte array before with the byte array after.

QKnxByteArray &QKnxByteArray::replace(quint8 before, quint8 after)

This function overloads replace().

Replaces every occurrence of the character before with the character after.

void QKnxByteArray::resize(int size)

Sets the size of the byte array to the number of bytes specified by size.

If size is greater than the current size, the byte array is extended to size bytes by adding default-constructed values to the end of the array.

If size is less than the current size, bytes are removed from the end of the array.

Returns a byte array that contains the number of rightmost bytes in the byte array specified by len.

The entire byte array is returned if len is greater than the value returned by size().

void QKnxByteArray::set(int i, quint8 val)

Sets the character at the index position i in the byte array to val.

i must be a valid index position in the byte array (that is, between 0 and the value returned by size()).

void QKnxByteArray::setValue(int i, quint8 val)

Sets the value at the index position i in the byte array to val.

If the index i is out of bounds, the function does nothing. If you are certain that i is within bounds, you can use set() instead, which is slightly faster.

See also value().

int QKnxByteArray::size() const

Returns the number of bytes in this byte array. The last byte in the byte array is at the position returned by this function minus 1.

bool QKnxByteArray::startsWith(const QKnxByteArray &ba) const

Returns true if this byte array starts with the byte array ba; otherwise returns false.

bool QKnxByteArray::startsWith(quint8 ch) const

This function overloads startsWith().

Returns true if this byte array starts with the character ch; otherwise returns false.

void QKnxByteArray::swap(QKnxByteArray &other)

Swaps the byte array other with this byte array. This operation is very fast and never fails.

const QByteArray &QKnxByteArray::toByteArray() const

Returns a copy of this byte array as QByteArray.

QKnxByteArray QKnxByteArray::toHex(quint8 separator = '\0') const

Returns a hex encoded copy of the byte array. The hex encoding uses the numbers 0-9 and the letters a-f.

If separator is not \0, the separator character is inserted between the hex bytes.

Example:

auto macAddress = QKnxByteArray::fromHex("123456abcdef");
macAddress.toHex(':'); // returns "12:34:56:ab:cd:ef"
macAddress.toHex(0);   // returns "123456abcdef"

quint8 QKnxByteArray::value(int i, quint8 defaultValue = {}) const

Returns the value at the index position i in the byte array.

If the index i is out of bounds, the function returns defaultValue, which may be a default-constructed value. If you are certain that i is within bounds, you can use at() instead, which is slightly faster.

See also setValue().

QKnxByteArray &QKnxByteArray::operator+=(const QKnxByteArray &ba)

Appends the byte array ba onto the end of this byte array and returns a reference to this byte array.

QKnxByteArray &QKnxByteArray::operator+=(quint8 ch)

This function overloads operator+=().

Appends the character ch onto the end of this byte array and returns a reference to this byte array.

Related Non-Members

uint qHash(const QKnxByteArray &key, uint seed = 0)

Returns the hash value for the key, using seed to seed the calculation.

bool operator!=(const QKnxByteArray &a1, const QKnxByteArray &a2)

Returns true if the byte array a1 is not equal to the byte array a2; otherwise returns false.

const QKnxByteArray operator+(const QKnxByteArray &a1, const QKnxByteArray &a2)

Returns a byte array that is the result of concatenating the byte array a1 and byte array a2.

const QKnxByteArray operator+(const QKnxByteArray &ba, quint8 ch)

This function overloads operator+().

Returns a byte array that is the result of concatenating the byte array ba and character ch.

const QKnxByteArray operator+(quint8 ch, const QKnxByteArray &ba)

This function overloads operator+().

Returns a byte array that is the result of concatenating the character ch and byte array ba.

QDebug operator<<(QDebug debug, const QKnxByteArray &byteArray)

Writes the byte array byteArray to the debug stream.

bool operator==(const QKnxByteArray &a1, const QKnxByteArray &a2)

Returns true if the byte array a1 is equal to the byte array a2; otherwise returns false.

© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.