CopperSpice API  1.9.1
QByteArray Class Reference

Stores a sequence of bytes. More...

Public Methods

 QByteArray ()
 
 QByteArray (const char *str, int size=-1)
 
 QByteArray (const QByteArray &other)
 
 QByteArray (int size, char ch)
 
 QByteArray (QByteArray &&other)
 
 ~QByteArray ()
 
QByteArray & append (char ch)
 
QByteArray & append (const char *str)
 
QByteArray & append (const char *str, int len)
 
QByteArray & append (const QByteArray &value)
 
char at (int i) const
 
iterator begin ()
 
const_iterator begin () const
 
int capacity () const
 
const_iterator cbegin () const
 
const_iterator cend () const
 
void chop (int n)
 
void clear ()
 
const_iterator constBegin () const
 
const char * constData () const
 
const_iterator constEnd () const
 
bool contains (char ch) const
 
bool contains (const char *str) const
 
bool contains (const QByteArray &value) const
 
int count () const
 
int count (char ch) const
 
int count (const char *str) const
 
int count (const QByteArray &value) const
 
const_reverse_iterator crbegin () const
 
const_reverse_iterator crend () const
 
char * data ()
 
const char * data () const
 
iterator end ()
 
const_iterator end () const
 
bool endsWith (char ch) const
 
bool endsWith (const char *str) const
 
bool endsWith (const QByteArray &value) const
 
QByteArray & fill (char ch, int size=-1)
 
int indexOf (char ch, int from=0) const
 
int indexOf (const char *str, int from=0) const
 
int indexOf (const QByteArray &value, int from=0) const
 
QByteArray & insert (int i, char ch)
 
QByteArray & insert (int i, const char *str)
 
QByteArray & insert (int i, const char *str, int len)
 
QByteArray & insert (int i, const QByteArray &value)
 
bool isEmpty () const
 
bool isNull () const
 
int lastIndexOf (char ch, int from=-1) const
 
int lastIndexOf (const char *str, int from=-1) const
 
int lastIndexOf (const QByteArray &value, int from=-1) const
 
QByteArray left (int len) const
 
QByteArray leftJustified (int width, char fill= ' ', bool truncate=false) const
 
int length () const
 
QByteArray mid (int pos, int len=-1) const
 
QByteArray & operator+= (char ch)
 
QByteArray & operator+= (const char *str)
 
QByteArray & operator+= (const QByteArray &value)
 
QByteArray & operator= (const char *str)
 
QByteArray & operator= (const QByteArray &other)
 
QByteArray & operator= (QByteArray &&other)
 
QByteRef operator[] (int i)
 
char operator[] (int i) const
 
QByteRef operator[] (uint i)
 
char operator[] (uint i) const
 
QByteArray & prepend (char ch)
 
QByteArray & prepend (const char *str)
 
QByteArray & prepend (const char *str, int len)
 
QByteArray & prepend (const QByteArray &value)
 
void push_back (char ch)
 
void push_back (const char *str)
 
void push_back (const QByteArray &value)
 
void push_front (char ch)
 
void push_front (const char *str)
 
void push_front (const QByteArray &value)
 
reverse_iterator rbegin ()
 
const_reverse_iterator rbegin () const
 
QByteArray & remove (char ch)
 
QByteArray & remove (int pos, int len)
 
reverse_iterator rend ()
 
const_reverse_iterator rend () const
 
QByteArray repeated (int times) const
 
QByteArray & replace (char before, char after)
 
QByteArray & replace (char before, const char *after)
 
QByteArray & replace (char before, const QByteArray &after)
 
QByteArray & replace (const char *before, const char *after)
 
QByteArray & replace (const char *before, const QByteArray &after)
 
QByteArray & replace (const char *before, int bsize, const char *after, int asize)
 
QByteArray & replace (const QByteArray &before, const char *after)
 
QByteArray & replace (const QByteArray &before, const QByteArray &after)
 
QByteArray & replace (int pos, int len, const char *str)
 
QByteArray & replace (int pos, int len, const char *str, int size)
 
QByteArray & replace (int pos, int len, const QByteArray &value)
 
void reserve (int size)
 
void resize (int size)
 
QByteArray right (int len) const
 
QByteArray rightJustified (int width, char fill= ' ', bool truncate=false) const
 
QByteArray & setNum (double n, char f= 'g', int prec=6)
 
QByteArray & setNum (float n, char f= 'g', int prec=6)
 
QByteArray & setNum (int n, int base=10)
 
QByteArray & setNum (qint64 n, int base=10)
 
QByteArray & setNum (quint64 n, int base=10)
 
QByteArray & setNum (short n, int base=10)
 
QByteArray & setNum (uint n, int base=10)
 
QByteArray & setNum (ushort n, int base=10)
 
QByteArray & setRawData (const char *str, int size)
 
QByteArray simplified () const
 
int size () const
 
QList< QByteArray > split (char sep) const
 
void squeeze ()
 
bool startsWith (char ch) const
 
bool startsWith (const char *str) const
 
bool startsWith (const QByteArray &value) const
 
void swap (QByteArray &other)
 
QByteArray toBase64 () const
 
CFDataRef toCFData () const
 
double toDouble (bool *ok=nullptr) const
 
float toFloat (bool *ok=nullptr) const
 
QByteArray toHex () const
 
int toInt (bool *ok=nullptr, int base=10) const
 
long toLong (bool *ok=nullptr, int base=10) const
 
qint64 toLongLong (bool *ok=nullptr, int base=10) const
 
QByteArray toLower () const
 
NSData * toNSData () const
 
QByteArray toPercentEncoding (const QByteArray &exclude=QByteArray (), const QByteArray &include=QByteArray (), char percent= '%') const
 
CFDataRef toRawCFData () const
 
NSData * toRawNSData () const
 
short toShort (bool *ok=nullptr, int base=10) const
 
uint toUInt (bool *ok=nullptr, int base=10) const
 
ulong toULong (bool *ok=nullptr, int base=10) const
 
quint64 toULongLong (bool *ok=nullptr, int base=10) const
 
QByteArray toUpper () const
 
ushort toUShort (bool *ok=nullptr, int base=10) const
 
QByteArray trimmed () const
 
void truncate (int pos)
 

Static Public Methods

static QByteArray fromBase64 (const QByteArray &value)
 
static QByteArray fromCFData (CFDataRef data)
 
static QByteArray fromHex (const QByteArray &value)
 
static QByteArray fromNSData (const NSData *data)
 
static QByteArray fromPercentEncoding (const QByteArray &value, char percent= '%')
 
static QByteArray fromRawCFData (CFDataRef data)
 
static QByteArray fromRawData (const char *str, int size)
 
static QByteArray fromRawNSData (const NSData *data)
 
static QByteArray number (double n, char f= 'g', int prec=6)
 
static QByteArray number (int n, int base=10)
 
static QByteArray number (qint64 n, int base=10)
 
static QByteArray number (quint64 n, int base=10)
 
static QByteArray number (uint n, int base=10)
 

Friends

QByteArray qUncompress (const uchar *data, int nbytes)
 

Related Functions

These are not member functions

bool operator!= (const char *a1, const QByteArray &a2)
 
bool operator!= (const QByteArray &a1, const char *a2)
 
bool operator!= (const QByteArray &a1, const QByteArray &a2)
 
const QByteArray operator+ (char a1, const QByteArray &a2)
 
const QByteArray operator+ (const char *a1, const QByteArray &a2)
 
const QByteArray operator+ (const QByteArray &a1, char a2)
 
const QByteArray operator+ (const QByteArray &a1, const char *a2)
 
const QByteArray operator+ (const QByteArray &a1, const QByteArray &a2)
 
bool operator< (const char *a1, const QByteArray &a2)
 
bool operator< (const QByteArray &a1, const char *a2)
 
bool operator< (const QByteArray &a1, const QByteArray &a2)
 
QDataStreamoperator<< (QDataStream &stream, const QByteArray &byteArray)
 
bool operator<= (const char *data, const QByteArray &value)
 
bool operator<= (const QByteArray &value, const char *data)
 
bool operator<= (const QByteArray &value1, const QByteArray &value2)
 
bool operator== (const char *ch, const QByteArray &value)
 
bool operator== (const QByteArray &value1, const char *value2)
 
bool operator== (const QByteArray &value1, const QByteArray &value2)
 
bool operator> (const char *ch, const QByteArray &value)
 
bool operator> (const QByteArray &value, const char *ch)
 
bool operator> (const QByteArray &value1, const QByteArray &value2)
 
bool operator>= (const char *ch, const QByteArray &value)
 
bool operator>= (const QByteArray &value, const char *ch)
 
bool operator>= (const QByteArray &value1, const QByteArray &value2)
 
QDataStreamoperator>> (QDataStream &stream, QByteArray &byteArray)
 
quint16 qChecksum (const char *data, uint len)
 
QByteArray qCompress (const QByteArray &data, int compressionLevel=-1)
 
QByteArray qCompress (const uchar *data, int nbytes, int compressionLevel=-1)
 
int qstrcmp (const char *str1, const char *str2)
 
char * qstrcpy (char *dst, const char *src)
 
char * qstrdup (const char *src)
 
int qstricmp (const char *str1, const char *str2)
 
uint qstrlen (const char *str)
 
int qstrncmp (const char *str1, const char *str2, uint len)
 
char * qstrncpy (char *dst, const char *src, uint len)
 
int qstrnicmp (const char *str1, const char *str2, uint len)
 
uint qstrnlen (const char *str, uint maxlen)
 
QByteArray qUncompress (const QByteArray &data)
 

Detailed Description

QByteArray can be used to store both raw data including embedded null characters and 8-bit null terminated strings. Internally a QByteArray will always end with a null terminator. As an alternative, the QString class is preferred for storing string data as 32 bit Unicode code points. QByteArray is the better choice when storing raw binary data.

QByteArray uses 0 based indexes. To access the element at a particular index position call operator[]().

Constructing a QByteArray

One way to initialize a QByteArray is to pass a const char * to the constructor. For example, the following code creates a byte array of size 5 containing the data "Hello". Calling the size() method will return 5 even though the array contains an extra '\0' character at the end.

QByteArray value("Hello");

Another way to create a QByteArray is to construct an empty QByteArray, call the resize() method, then initialize the data one byte at a time.

QByteArray value;
value.resize(5);
value[0] = 0x3c;
value[1] = 0xb8;
value[2] = 0x64;
value[3] = 0x18;
value[4] = 0xca;

For read only access an alternative syntax is to use at().

for (int i = 0; i < value.size(); ++i) {
if (value.at(i) >= 'a' && value.at(i) <= 'f') {
cout << "Found character in range [a-f]" << endl;
}
}

To extract multiple bytes at a time use the methods left(), right(), or mid().

Null Termination

A QByteArray can contain null bytes. The size() method always returns the size of the whole array, including embedded null bytes. If you want to obtain the length of the data up to and excluding the first null character, call qstrlen() on the byte array.

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 methods return a pointer to the beginning of the data. The pointer is guaranteed to remain valid until a non-const method is called on the QByteArray. It is also guaranteed that the data ends with a null byte unless the QByteArray was created from a raw data. The null terminator is automatically provided by QByteArray and is not counted in size().

Data Manipulation

QByteArray provides the following basic methods for modifying the byte data: append(), prepend(), insert(), replace(), and remove().

QByteArray x("and");
x.prepend("rock "); // x == "rock and"
x.append(" roll"); // x == "rock and roll"
x.replace(5, 3, "&"); // x == "rock &roll"

The replace() and remove() methods first two arguments are the position from which to start erasing and the number of bytes that should be erased.

When you append() data to a non-empty array the array will be reallocated and the new data copied to it. You can avoid this behavior by calling reserve(), which preallocates a certain amount of memory. You can also call capacity() to find out how much memory QByteArray actually allocated. Data appended to an empty array is not copied.

A frequent requirement is to remove whitespace characters from a byte array ('\n', '\t', ' ', etc.). If you want to remove whitespace from both ends of a QByteArray, use trimmed(). If you want to remove whitespace from both ends and replace multiple consecutive whitespaces with a single space character within the byte array, use simplified().

Searching

If you want to find all occurrences of a particular character or substring in a QByteArray, use indexOf() or lastIndexOf(). The former searches forward starting from a given index position, the latter searches backward. Both return the index position of the character or substring if found, otherwise they return -1. This loop finds all occurrences of a particular substring.

QByteArray data("To dream the impossible dream. . .");
int pos = 0;
while ((pos = data.indexOf("dream", pos)) != -1) {
cout << "Found 'dream' at index position " << pos << endl;
++pos;
}

To check whether a QByteArray contains a particular character or substring, use contains(). If you want to find out how many times a particular character or substring occurs in the byte array, use count(). If you want to replace all occurrences of a particular value with another, use one of the two-parameter replace() overloads.

Comparison

A QByteArray can be compared using overloaded operators such as operator<(), operator<=(), operator==(), operator>=(), and so on. The comparison is based exclusively on the numeric values of the characters and is very fast, but is not what a human would expect. QString8::localeAwareCompare() is a better choice for sorting user-interface strings.

QByteArray distinguishes between a null byte array and an empty byte array. A null byte array is a byte array that is initialized using QByteArray's default constructor or by passing (const char *)0 to the constructor. An empty byte array is any byte array with size 0. A null byte array is always empty but an empty byte array is not necessarily null.

QByteArray().isNull(); // returns true
QByteArray().isEmpty(); // returns true
QByteArray("").isNull(); // returns false
QByteArray("").isEmpty(); // returns true
QByteArray("abc").isNull(); // returns false
QByteArray("abc").isEmpty(); // returns false

All methods except isNull() treat null byte arrays the same as empty byte arrays. For example, data() returns a valid pointer to a null character for a null byte array and QByteArray() compares equal to QByteArray(""). You should normally use isEmpty() instead of isNull().

Number-String Conversions

Methods that perform conversions between numeric data types and strings are performed in the C locale regardless of the user's locale settings. Use QString to perform locale-aware conversions between numbers and strings.

8-bit Character Comparisons

In QByteArray the notion of uppercase and lowercase and of which character is greater than or less than another character is locale dependent. This affects methods which support a case insensitive option or that compare or lowercase or uppercase their arguments.

Case insensitive operations and comparisons will only be accurate if both strings contain only ASCII characters.

Removed or Unsupported Methods

The following methods and typedefs are unsafe.

Obsolete MethodReplace With
qsnprintf() Since C variadic functions are not type safe use QTextStream or QStringParser::formatArgs
qvsnprintf() Since C variadic functions are not type safe use QTextStream or QStringParser::formatArgs

The following methods or overloads on QByteArray have been removed since they only work with Latin-1 characters. Replacement syntax is shown for each method.

Obsolete MethodReplace With
QByteArray::append(const QString &str) data.append(str.toLatin1())
QByteArray::insert(int i, const QString &str) data.insert(i, str.toLatin1())
QByteArray::replace(char ch, const QString &after) data.replace(ch, after.toLatin1())
QByteArray::replace(const QString &before, const char *after) data.replace(before.toLatin1(), after)
QByteArray::replace(const QString &before, const QByteArray &after) data.replace(before.toLatin1(), after)
QByteArray::indexOf(const QString &str, int from) const data.indexOf(str.toLatin1(), from)
QByteArray::lastIndexOf(const QString &str, int from) const data.lastIndexOf(str.toLatin1(), from)
QByteArray::operator+=(const QString &str) data += str.toLatin1()
QByteArray::operator==(const QString &str) const data == str.toLatin1()
QByteArray::operator!=(const QString &str) const data != str.toLatin1()
QByteArray::operator<(const QString &str) const data < str.toLatin1()
QByteArray::operator>(const QString &str) const data > str.toLatin1()
QByteArray::operator<=(const QString &str) const data <= str.toLatin1()
QByteArray::operator>=(const QString &str) const data >= str.toLatin1()

Constructor & Destructor Documentation

QByteArray::QByteArray ( )
inline

Constructs an empty byte array.

See also
isEmpty()
QByteArray::QByteArray ( const char *  str,
int  size = -1 
)

Constructs a byte array containing the first size bytes of array str. If data is 0 a null byte array is constructed.

QByteArray makes a deep copy of the string data.

See also
fromRawData()
QByteArray::QByteArray ( int  size,
char  ch 
)

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

See also
fill()
QByteArray::QByteArray ( const QByteArray &  other)
inline

Copy constructs a new QByteArray from other.

See also
operator=()
QByteArray::QByteArray ( QByteArray &&  other)
inline

Move constructs a new QByteArray from other.

QByteArray::~QByteArray ( )
inline

Destroys the byte array.

Method Documentation

QByteArray & QByteArray::append ( char  ch)

Appends the character ch to this byte array.

QByteArray & QByteArray::append ( const char *  str)

Appends the string str to this byte array.

QByteArray & QByteArray::append ( const char *  str,
int  len 
)

Appends the first len characters of the string str to this byte array and returns a reference to this byte array.

If len is negative then the length of the string will be determined using qstrlen(). If len is zero or str is a null pointer nothing is appended to the byte array. Your code must ensure len is not larger than the length of str.

QByteArray & QByteArray::append ( const QByteArray &  value)

Appends the byte array value onto the end of this byte array. Equivalent to calling insert(size(), value).

QByteArray x("free");
QByteArray y("dom");
x.append(y); // x == "freedom"

QByteArray is an implicitly shared class.

See also
operator+=(), prepend(), insert()
char QByteArray::at ( int  i) const
inline

Returns the character at index position i in the byte array. The value for i must be a valid index position in the byte array.

See also
operator[]()
QByteArray::iterator QByteArray::begin ( )
inline

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

See also
constBegin(), end()
QByteArray::const_iterator QByteArray::begin ( ) const
inline

This method overloads begin().

int QByteArray::capacity ( ) const
inline

Returns the maximum number of bytes that can be stored in the byte array without forcing a reallocation.

The sole purpose of this method is to provide a means of fine tuning QByteArray's memory usage. In general, you will rarely ever need to call this method. If you want to know how many bytes are in the byte array, call size().

See also
reserve(), squeeze()
QByteArray::const_iterator QByteArray::cbegin ( ) const
inline

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

See also
begin(), cend()
QByteArray::const_iterator QByteArray::cend ( ) const
inline

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

See also
cbegin(), end()
void QByteArray::chop ( int  n)

Removes n bytes from the end of the byte array. If n is greater than size(), the result is an empty byte array.

QByteArray value("STARTTLS\r\n");
value.chop(2); // value == "STARTTLS"
See also
truncate(), resize(), left()
void QByteArray::clear ( )

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

See also
resize(), isEmpty()
QByteArray::const_iterator QByteArray::constBegin ( ) const
inline

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

See also
begin(), constEnd()
const char * QByteArray::constData ( ) const
inline

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 unless the QByteArray object was created from raw data. The pointer remains valid as long as the byte array is not reallocated or destroyed.

This method is mostly useful to pass a byte array to a method that accepts a const char *.

A QByteArray can store any byte values including null. Most methods which take char * arguments assume the data ends at the first null character they encounter.

See also
data(), operator[](), fromRawData()
QByteArray::const_iterator QByteArray::constEnd ( ) const
inline

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

See also
constBegin(), end()
bool QByteArray::contains ( char  ch) const
inline

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

bool QByteArray::contains ( const char *  str) const
inline

Returns true if the byte array contains the string str, otherwise returns false.

bool QByteArray::contains ( const QByteArray &  value) const
inline

Returns true if the byte array contains an occurrence of the byte array value, otherwise returns false.

See also
indexOf(), count()
int QByteArray::count ( ) const
inline

Equivalent to calling size().

int QByteArray::count ( char  ch) const

Returns the number of occurrences of character ch in the byte array.

See also
contains(), indexOf()
int QByteArray::count ( const char *  str) const

Returns the number of (potentially overlapping) occurrences of string str in the byte array.

int QByteArray::count ( const QByteArray &  value) const

Returns the number of (potentially overlapping) occurrences of byte array value in this byte array.

See also
contains(), indexOf()
const_reverse_iterator QByteArray::crbegin ( ) const
inline

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

See also
begin(), rbegin(), rend()
const_reverse_iterator QByteArray::crend ( ) const
inline

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

See also
end(), rend(), rbegin()
char * QByteArray::data ( )
inline

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 so the number of bytes in the returned character string is size() + 1 for the terminator. The pointer remains valid as long as the byte array is not reallocated or destroyed.

QByteArray value("Hello world");
char *data = value.data();
while (*data) {
cout << "[" << *data << "]" << endl;
++data;
}
See also
constData(), operator[]()
const char * QByteArray::data ( ) const
inline

This is an overloaded method.

QByteArray::iterator QByteArray::end ( )
inline

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

See also
begin(), constEnd()
QByteArray::const_iterator QByteArray::end ( ) const
inline

This method overloads end().

bool QByteArray::endsWith ( char  ch) const

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

bool QByteArray::endsWith ( const char *  str) const

Returns true if this byte array ends with string str, otherwise returns false.

bool QByteArray::endsWith ( const QByteArray &  value) const

Returns true if this byte array ends with byte array value, otherwise returns false.

QByteArray url("https://www.copperspice.com/index.html");
if (url.endsWith(".html")) {
// do something
}
See also
startsWith(), right()
QByteArray & QByteArray::fill ( char  ch,
int  size = -1 
)

Sets every byte in the byte array to character ch. If size is different from -1 (the default), the byte array is resized to size beforehand.

QByteArray value("Istambul");
value.fill('o'); // value == "oooooooo"
value.fill('X', 2); // value == "XX"
See also
resize()
QByteArray QByteArray::fromBase64 ( const QByteArray &  value)
static

Returns a decoded copy of the Base64 array value. Input is not checked for validity. Invalid characters in the input are skipped, enabling the decoding process to continue with subsequent characters. The following algorithm is used to decode Base64-encoded data as defined in RFC 2045.

QByteArray value = QByteArray::fromBase64("Q29wcGVyU3BpY2UgaXMgd29uZGVyZnVsIQ==");
value.data(); // returns "CopperSpice is wonderful!"
See also
toBase64()
QByteArray QByteArray::fromCFData ( CFDataRef  data)
static

Constructs a new QByteArray containing a copy of the CFData data.

See also
fromRawCFData(), fromRawData(), toRawCFData(), toCFData()
QByteArray QByteArray::fromHex ( const QByteArray &  value)
static

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

QByteArray value = QByteArray::fromHex("436F70706572737069636520697320776F6E64657266757C21");
value.data(); // returns "CopperSpice is wonderful!"
See also
toHex()
QByteArray QByteArray::fromNSData ( const NSData *  data)
static

Constructs a new QByteArray containing a copy of the NSData data.

See also
fromRawNSData(), fromRawData(), toNSData(), toRawNSData()
QByteArray QByteArray::fromPercentEncoding ( const QByteArray &  value,
char  percent = '%' 
)
static

Returns a decoded copy of the URI/URL-style percent-encoded value. The percent parameter allows you to replace the '%' character for another (for instance, '_' or '=').

QByteArray value = QByteArray::fromPercentEncoding("CopperSpice%20is%20wonderful%33");
value.data(); // returns "CopperSpice is wonderful!"
See also
toPercentEncoding(), QUrl::fromPercentEncoding()
QByteArray QByteArray::fromRawCFData ( CFDataRef  data)
static

Constructs a QByteArray that uses the bytes of the CFData data. The data is not copied.

The caller guarantees that the CFData will not be deleted or modified as long as this QByteArray object exists.

See also
fromCFData(), fromRawData(), toRawCFData(), toCFData()
QByteArray QByteArray::fromRawData ( const char *  str,
int  size 
)
static

Constructs a QByteArray that uses the first size bytes of the str array. The data is not copied. The caller guarantees the data will not be deleted or modified as long as this QByteArray and any copies of it exist.

Since QByteArray does not take ownership of the data the destructor will never delete the raw data, even when the last QByteArray referring to data is destroyed. A subsequent attempt to modify the contents of the returned QByteArray or any copy made from it will cause it to create a deep copy of the data array before doing the modification. This ensures that the raw data array itself will never be modified by QByteArray.

The following example of how to read data using a QDataStream on raw data in memory without copying the raw data into a QByteArray.

static const char mydata[] = {
0x00, 0x00, 0x03, 0x84, 0x78, 0x9c, 0x3b, 0x76,
0xec, 0x18, 0xc3, 0x31, 0x0a, 0xf1, 0xcc, 0x99,
...
0x6d, 0x5b
};
QByteArray data = QByteArray::fromRawData(mydata, sizeof(mydata));
QDataStream in(&data, QIODevice::ReadOnly);
...
Warning
A byte array created with fromRawData() will not be null terminated unless the raw data contains a null character at the end.
See also
setRawData(), data(), constData()
QByteArray QByteArray::fromRawNSData ( const NSData *  data)
static

Constructs a QByteArray that uses the bytes of the NSData data. The data is not copied.

The caller guarantees the NSData will not be deleted or modified as long as this QByteArray object exists.

See also
fromNSData(), fromRawData(), toRawNSData(), toNSData()
int QByteArray::indexOf ( char  ch,
int  from = 0 
) const

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

QByteArray value("ABCBA");
value.indexOf("B"); // returns 1
value.indexOf("B", 1); // returns 1
value.indexOf("B", 2); // returns 3
value.indexOf("X"); // returns -1
See also
lastIndexOf(), contains()
int QByteArray::indexOf ( const char *  str,
int  from = 0 
) const

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

int QByteArray::indexOf ( const QByteArray &  value,
int  from = 0 
) const

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

QByteArray x("sticky question");
QByteArray y("sti");
x.indexOf(y); // returns 0
x.indexOf(y, 1); // returns 10
x.indexOf(y, 10); // returns 10
x.indexOf(y, 11); // returns -1
See also
lastIndexOf(), contains(), count()
QByteArray & QByteArray::insert ( int  i,
char  ch 
)

Inserts character ch at index position i in the byte array. If the value for i is greater than size(), the array is first extended using resize().

QByteArray & QByteArray::insert ( int  i,
const char *  str 
)

Inserts the string str at position i in the byte array. If the value for i is greater than size(), the array is first extended using resize().

QByteArray & QByteArray::insert ( int  i,
const char *  str,
int  len 
)

Inserts len bytes of the string str at position i in the byte array. If the value for i is greater than size(), the array is first extended using resize().

QByteArray & QByteArray::insert ( int  i,
const QByteArray &  value 
)

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

QByteArray value("Meal");
value.insert(1, QByteArray("ontr")); // value == "Montreal"
See also
append(), prepend(), replace(), remove()
bool QByteArray::isEmpty ( ) const
inline

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

QByteArray().isEmpty(); // returns true
QByteArray("").isEmpty(); // returns true
QByteArray("abc").isEmpty(); // returns false
See also
size()
bool QByteArray::isNull ( ) const

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

QByteArray().isNull(); // returns true
QByteArray("").isNull(); // returns false
QByteArray("abc").isNull(); // returns false

CopperSpice makes a distinction between null byte arrays and empty byte arrays for historical reasons. For most applications, what matters is whether or not a byte array contains any data, and this can be determined using isEmpty().

See also
isEmpty()
int QByteArray::lastIndexOf ( char  ch,
int  from = -1 
) const

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

QByteArray value("ABCBA");
value.lastIndexOf("B"); // returns 3
value.lastIndexOf("B", 3); // returns 3
value.lastIndexOf("B", 2); // returns 1
value.lastIndexOf("X"); // returns -1
See also
indexOf(), contains()
int QByteArray::lastIndexOf ( const char *  str,
int  from = -1 
) const

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

int QByteArray::lastIndexOf ( const QByteArray &  value,
int  from = -1 
) const

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

QByteArray x("crazy azimuths");
QByteArray y("az");
x.lastIndexOf(y); // returns 6
x.lastIndexOf(y, 6); // returns 6
x.lastIndexOf(y, 5); // returns 2
x.lastIndexOf(y, 1); // returns -1
See also
indexOf(), contains(), count()
QByteArray QByteArray::left ( int  len) const

Returns a byte array that contains the leftmost len bytes of this byte array. The entire byte array is returned if len is greater than size().

QByteArray x("Pineapple");
QByteArray y = x.left(4); // y == "Pine"
See also
right(), mid(), startsWith(), truncate()
QByteArray QByteArray::leftJustified ( int  width,
char  fill = ' ',
bool  truncate = false 
) const

Returns a byte array of size width that contains this byte array padded by the fill character. If truncate is false and the size() of the byte array is more than width, then the returned byte array is a copy of this byte array.

If truncate is true and the size() of the byte array is more than width, then any bytes in a copy of the byte array after position width are removed, and the copy is returned.

QByteArray x("apple");
QByteArray y = x.leftJustified(8, '.'); // y == "apple..."
See also
rightJustified()
int QByteArray::length ( ) const
inline

Equivalent to calling size().

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

Returns a byte array containing len bytes from this byte array, starting at position pos.

If len is -1 (the default), or pos + len >= size(), this method returns a byte array containing all bytes starting at position pos until the end of the byte array.

QByteArray x("Five pineapples");
QByteArray y = x.mid(5, 4); // y == "pine"
QByteArray z = x.mid(5); // z == "pineapples"
See also
left(), right()
QByteArray QByteArray::number ( double  n,
char  f = 'g',
int  prec = 6 
)
static

Returns a byte array which contains the printed value of n, using the specified format f with precision prec. The value for n is formatted according to the f format specified, which is g by default, and can be any of the following.

FormatMeaning
eformat as [-]9.9e[+|-]999
Eformat as [-]9.9E[+|-]999
fformat as [-]9.9
guse e or f format, whichever is the most concise
Guse E or f format, whichever is the most concise

With 'e', 'E', and 'f', prec is the number of digits after the decimal point. With 'g' and 'G', prec is the maximum number of significant digits (trailing zeroes are omitted).

QByteArray value = QByteArray::number(12.3456, 'E', 3); // value == 1.235E+01
Note
The conversion of the number is performed in the default C locale regardless of the user's locale.
See also
toDouble()
QByteArray QByteArray::number ( int  n,
int  base = 10 
)
static

Returns a byte array containing the string equivalent of the number n to base (10 by default). The base can be any value between 2 and 36.

int n = 63;
QByteArray::number(n); // returns "63"
QByteArray::number(n, 16); // returns "3f"
QByteArray::number(n, 16).toUpper(); // returns "3F"
Note
The conversion of the number is performed in the default C locale regardless of the user's locale.
See also
setNum(), toInt()
QByteArray QByteArray::number ( qint64  n,
int  base = 10 
)
static

This is an overloaded method.

See also
toLongLong()
QByteArray QByteArray::number ( quint64  n,
int  base = 10 
)
static

This is an overloaded method.

See also
toULongLong()
QByteArray QByteArray::number ( uint  n,
int  base = 10 
)
static

This is an overloaded method.

See also
toUInt()
QByteArray & QByteArray::operator+= ( char  ch)
inline

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

QByteArray & QByteArray::operator+= ( const char *  str)
inline

Appends the string>/i> onto the end of this byte array and returns a reference to this byte array.

QByteArray & QByteArray::operator+= ( const QByteArray &  value)
inline

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

QByteArray x("free");
QByteArray y("dom");
x += y; // x == "freedom"

QByteArray is an implicitly shared class.

See also
append(), prepend()
QByteArray & QByteArray::operator= ( const char *  str)

Assigns str to this byte array and returns a reference to this object.

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

Copy assigns from other and returns a reference to this object.

QByteArray & QByteArray::operator= ( QByteArray &&  other)
inline

Move assigns from other and returns a reference to this object.

QByteRef QByteArray::operator[] ( int  i)
inline

Returns a reference to the data at index position i. If an assignment is made beyond the end of the byte array, the array is extended with resize() before the assignment takes place.

QByteArray value;
for (int i = 0; i < 10; ++i) {
value[i] = 'A' + i; // value == "ABCDEFGHIJ"
}

The return value is of type QByteRef, a helper class for QByteArray. When you get an object of type QByteRef, you can use it as if it were a char &. If you assign to it, the assignment will apply to the character in the QByteArray from which you got the reference.

See also
at()
char QByteArray::operator[] ( int  i) const
inline

Equivalent to calling at(i).

QByteRef QByteArray::operator[] ( uint  i)
inline

This is an overloaded method.

char QByteArray::operator[] ( uint  i) const
inline

This is an overloaded method.

QByteArray & QByteArray::prepend ( char  ch)

Prepends the character ch to this byte array.

QByteArray & QByteArray::prepend ( const char *  str)

Prepends the string str to this byte array.

QByteArray & QByteArray::prepend ( const char *  str,
int  len 
)

Prepends len bytes of the string str to this byte array.

QByteArray & QByteArray::prepend ( const QByteArray &  value)

Prepends the byte array value to this byte array and returns a reference to this byte array. Equivalent to calling insert(0, value).

QByteArray x("ship");
QByteArray y("air");
x.prepend(y); // x == "airship"

QByteArray is an implicitly shared class.

See also
append(), insert()
void QByteArray::push_back ( char  ch)
inline

Equivalent to calling append(ch).

void QByteArray::push_back ( const char *  str)
inline

Equivalent to calling append(str).

void QByteArray::push_back ( const QByteArray &  value)
inline

Equivalent to calling append(value).

void QByteArray::push_front ( char  ch)
inline

Equivalent to calling prepend(ch).

void QByteArray::push_front ( const char *  str)
inline

Equivalent to calling prepend(str).

void QByteArray::push_front ( const QByteArray &  value)
inline

Equivalent to calling prepend(value).

reverse_iterator QByteArray::rbegin ( )
inline

This is an overloaded method.

const_reverse_iterator QByteArray::rbegin ( ) const
inline

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

See also
begin(), crbegin(), rend()
QByteArray & QByteArray::remove ( char  ch)

Removes all instances of the character ch from the current array.

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

Removes len bytes from the array, starting at index position pos, returns a reference to the array.

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

QByteArray value("Montreal");
value.remove(1, 4); // value == "Meal"
See also
insert(), replace()
reverse_iterator QByteArray::rend ( )
inline

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

See also
end(), crend(), rbegin()
const_reverse_iterator QByteArray::rend ( ) const
inline

This is an overloaded method.

QByteArray QByteArray::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.

QByteArray value("ab");
value.repeated(4); // returns "abababab"
QByteArray & QByteArray::replace ( char  before,
char  after 
)

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

QByteArray & QByteArray::replace ( char  before,
const char *  after 
)
inline

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

QByteArray & QByteArray::replace ( char  before,
const QByteArray &  after 
)

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

QByteArray & QByteArray::replace ( const char *  before,
const char *  after 
)
inline

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

QByteArray & QByteArray::replace ( const char *  before,
const QByteArray &  after 
)

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

QByteArray & QByteArray::replace ( const char *  before,
int  bsize,
const char *  after,
int  asize 
)

Replaces every occurrence of the string before with the string after. Since the sizes of the strings are given by bsize and asize, they may contain null characters and do not need to be null terminated.

QByteArray & QByteArray::replace ( const QByteArray &  before,
const char *  after 
)
inline

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

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

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

QByteArray value("colour behavior flavour neighbour");
value.replace(QByteArray("ou"), QByteArray("o")); // value == "color behavior flavor neighbor"
QByteArray & QByteArray::replace ( int  pos,
int  len,
const char *  str 
)

Replaces len bytes from index position pos with the null terminated string str.

QByteArray & QByteArray::replace ( int  pos,
int  len,
const char *  str,
int  size 
)

Replaces len bytes from index position pos with size bytes from the string str. The value for str is allowed to contain null characters.

QByteArray & QByteArray::replace ( int  pos,
int  len,
const QByteArray &  value 
)

Replaces len bytes from index position pos with the byte array value. Returns a reference to this byte array.

QByteArray x("Say yes.");
QByteArray y("no");
x.replace(4, 3, y); // x == "Say no."
See also
insert(), remove()
void QByteArray::reserve ( int  size)
inline

Attempts to allocate memory for at least size bytes. If you know in advance how large the byte array will be, you can call this method, and if you call resize() often you are likely to get better performance. If size is an underestimate, the worst that will happen is that the QByteArray will be a bit slower.

The sole purpose of this method is to provide a means of fine tuning QByteArray's memory usage. In general, you will rarely ever need to call this method. If you want to change the size of the byte array, call resize().

See also
squeeze(), capacity()
void QByteArray::resize ( int  size)

Sets the size of the byte array to size bytes.

If size is greater than the current size, the byte array is extended to make it size bytes with the extra bytes added to the end. The new bytes are uninitialized. If size is less than the current size, bytes are removed from the end.

See also
size(), truncate()
QByteArray QByteArray::right ( int  len) const

Returns a byte array that contains the rightmost len bytes of this byte array. The entire byte array is returned if len is greater than size().

QByteArray x("Pineapple");
QByteArray y = x.right(5); // y == "apple"
See also
endsWith(), left(), mid()
QByteArray QByteArray::rightJustified ( int  width,
char  fill = ' ',
bool  truncate = false 
) const

Returns a byte array of size width that contains the fill character followed by this byte array. If truncate is false and the size of the byte array is more than width, then the returned byte array is a copy of this byte array.

If truncate is true and the size of the byte array is more than width, then the resulting byte array is truncated at position width.

QByteArray x("apple");
QByteArray y = x.rightJustified(8, '.'); // y == "...apple"
See also
leftJustified()
QByteArray & QByteArray::setNum ( double  n,
char  f = 'g',
int  prec = 6 
)

Sets the byte array to the printed value of n, formatted in format f with precision prec, and returns a reference to the byte array.

The format f can be any of the following:

FormatMeaning
eformat as [-]9.9e[+|-]999
Eformat as [-]9.9E[+|-]999
fformat as [-]9.9
guse e or f format, whichever is the most concise
Guse E or f format, whichever is the most concise

With 'e', 'E', and 'f', prec is the number of digits after the decimal point. With 'g' and 'G', prec is the maximum number of significant digits (trailing zeroes are omitted).

Note
The conversion of the number is performed in the default C locale regardless of the user's locale.
See also
toDouble()
QByteArray & QByteArray::setNum ( float  n,
char  f = 'g',
int  prec = 6 
)
inline

Sets the byte array to the printed value of n, formatted in format f with precision prec, and returns a reference to the byte array.

Note
The conversion of the number is performed in the default C locale regardless of the user's locale.
See also
toFloat()
QByteArray & QByteArray::setNum ( int  n,
int  base = 10 
)
inline

Sets the byte array to the printed value of n in base (10 by default) and returns a reference to the byte array. The base can be any value between 2 and 36.

QByteArray value;
int n = 63;
value.setNum(n); // value == "63"
value.setNum(n, 16); // value == "3f"
Note
The conversion of the number is performed in the default C locale regardless of the user's locale.
See also
number(), toInt()
QByteArray & QByteArray::setNum ( qint64  n,
int  base = 10 
)

This is an overloaded method.

See also
toLongLong()
QByteArray & QByteArray::setNum ( quint64  n,
int  base = 10 
)

This is an overloaded method.

See also
toULongLong()
QByteArray & QByteArray::setNum ( short  n,
int  base = 10 
)
inline

This is an overloaded method.

See also
toShort()
QByteArray & QByteArray::setNum ( uint  n,
int  base = 10 
)
inline

This is an overloaded method.

See also
toUInt()
QByteArray & QByteArray::setNum ( ushort  n,
int  base = 10 
)
inline

This is an overloaded method.

See also
toUShort()
QByteArray & QByteArray::setRawData ( const char *  str,
int  size 
)

Resets the QByteArray to use the first size bytes of the data array.

The bytes are not copied. Users must guarantee the data will not be deleted or modified as long as this QByteArray and any copies of it exist. This method can be used instead of fromRawData() to re-use existing QByteArray objects to save memory re-allocations.

See also
fromRawData(), data(), constData()
QByteArray QByteArray::simplified ( ) const

Returns a byte array that has whitespace removed from the start and the end, and which has each sequence of internal whitespace replaced with a single space.

Whitespace means any character for which the standard C++ isspace() method returns true. This includes the ASCII characters '\t', '\n', '\v', '\f', '\r', and ' '.

QByteArray value(" lots\t of\nwhitespace\r\n ");
value = value.simplified(); // value == "lots of whitespace";
See also
trimmed()
int QByteArray::size ( ) const
inline

Returns the number of bytes in this byte array.

The last byte in the byte array is at position size() - 1. QByteArray ensures the byte at position size() is always a null character. You can use the return value of data() and constData() as arguments to methods which expect null terminated strings. If the QByteArray object was created from raw data which did not include the trailing null termination, then QByteArray does not add it automatically.

QByteArray value("Hello");
int n = value.size(); // n == 5
value.data()[0]; // returns 'H'
value.data()[4]; // returns 'o'
value.data()[5]; // returns '\0'
See also
isEmpty(), resize()
QList< QByteArray > QByteArray::split ( char  sep) const

Splits the byte array into subarrays wherever sep occurs, and returns the list of those arrays. If sep does not match anywhere in the byte array, split() returns a single-element list containing this byte array.

void QByteArray::squeeze ( )
inline

Releases any memory not required to store the array's data.

The sole purpose of this method is to provide a means of fine tuning QByteArray's memory usage. In general, you will rarely ever need to call this method.

See also
reserve(), capacity()
bool QByteArray::startsWith ( char  ch) const

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

bool QByteArray::startsWith ( const char *  str) const

Returns true if this byte array starts with string str, otherwise returns false.

bool QByteArray::startsWith ( const QByteArray &  value) const

Returns true if this byte array starts with byte array value, otherwise returns false.

QByteArray url("ftp://ftp.copperspice.com/");
if (url.startsWith("ftp:"))
...
See also
endsWith(), left()
void QByteArray::swap ( QByteArray &  other)
inline

Swaps other with this object. This operation is very fast and never fails.

QByteArray QByteArray::toBase64 ( ) const

Returns a copy of the byte array, encoded as Base64.

QByteArray text("CopperSpice is wonderful!");
text.toBase64(); // returns "Q29wcGVyU3BpY2UgaXMgd29uZGVyZnVsIQ=="

The algorithm used to encode Base64-encoded data is defined in RFC 2045.

See also
fromBase64()
CFDataRef QByteArray::toCFData ( ) const

Creates a CFData from a QByteArray. The caller owns the CFData object and is responsible for releasing it.

See also
toRawCFData(), fromCFData(), fromRawCFData(), fromRawData()
double QByteArray::toDouble ( bool *  ok = nullptr) const

Returns the byte array converted to a double value. Returns 0.0 if the conversion fails. If ok is not a nullptr, this method will set the value according to the success of the conversion. The value is false to indicate the conversion failed, otherwise ok is set to true.

QByteArray value("1234.56");
double a = value.toDouble(); // a == 1234.56
Note
The conversion of the number is performed in the default C locale regardless of the user's locale.
See also
number()
float QByteArray::toFloat ( bool *  ok = nullptr) const

Returns the byte array converted to a float value. Returns 0.0 if the conversion fails. If ok is not a nullptr, this method will set the value according to the success of the conversion. The value is false to indicate the conversion failed, otherwise ok is set to true.

Note
The conversion of the number is performed in the default C locale regardless of the user's locale.
See also
number()
QByteArray QByteArray::toHex ( ) const

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

See also
fromHex()
int QByteArray::toInt ( bool *  ok = nullptr,
int  base = 10 
) const

Returns the byte array converted to an int using base, which is 10 by default and must be between 2 and 36, or 0. Returns 0 if the conversion fails.

If base is 0 it is determined using the following rules: (1) If the byte array begins with "0x" it is assumed to be hexadecimal (2) If it begins with "0" it is assumed to be octal, (3) otherwise it is assumed to be decimal.

If ok is not a nullptr, this method will set the value according to the success of the conversion. The value is false to indicate the conversion failed, otherwise ok is set to true.

QByteArray str("FF");
bool ok;
int hex = str.toInt(&ok, 16); // hex == 255, ok == true
int dec = str.toInt(&ok, 10); // dec == 0, ok == false
Note
The conversion of the number is performed in the default C locale regardless of the user's locale.
See also
number()
long QByteArray::toLong ( bool *  ok = nullptr,
int  base = 10 
) const

Returns the byte array converted to a long int using base, which is 10 by default and must be between 2 and 36, or 0. The method returns 0 if the conversion fails.

If base is 0 it is determined using the following rules: (1) If the byte array begins with "0x" it is assumed to be hexadecimal (2) If it begins with "0" it is assumed to be octal, (3) otherwise it is assumed to be decimal.

If ok is not a nullptr, this method will set the value according to the success of the conversion. The value is false to indicate the conversion failed, otherwise ok is set to true.

QByteArray str("FF");
bool ok;
long hex = str.toLong(&ok, 16); // hex == 255, ok == true
long dec = str.toLong(&ok, 10); // dec == 0, ok == false
Note
The conversion of the number is performed in the default C locale regardless of the user's locale.
See also
number()
qint64 QByteArray::toLongLong ( bool *  ok = nullptr,
int  base = 10 
) const

Returns the byte array converted to a long long using base, which is 10 by default and must be between 2 and 36, or 0. Returns 0 if the conversion fails.

If base is 0 it is determined using the following rules: (1) If the byte array begins with "0x" it is assumed to be hexadecimal (2) If it begins with "0" it is assumed to be octal, (3) otherwise it is assumed to be decimal.

If ok is not a nullptr, this method will set the value according to the success of the conversion. The value is false to indicate the conversion failed, otherwise ok is set to true.

Note
The conversion of the number is performed in the default C locale regardless of the user's locale.
See also
number()
QByteArray QByteArray::toLower ( ) const

Returns a lowercase copy of the byte array. The byte array is interpreted as a Latin-1 encoded string.

QByteArray x("CopperSpice");
QByteArray y = x.toLower(); // y == "copperspice"
See also
toUpper(), 8-bit Character Comparisons
NSData * QByteArray::toNSData ( ) const

Creates a NSData from a QByteArray. The NSData object is autoreleased.

See also
fromNSData(), fromRawNSData(), fromRawData(), toRawNSData()
QByteArray QByteArray::toPercentEncoding ( const QByteArray &  exclude = QByteArray(),
const QByteArray &  include = QByteArray(),
char  percent = '%' 
) const

Returns a URL style "percent encoded" copy of the current QByteArray. The default character for percent is a percent sign. This method will encode every character except those listed below.

  • Upper and lower case letters
  • Digits 0 to 9
  • Punctuation characters dash, dot, underscore, and tilde

To prevent a character from being "percent encoded" a QByteArray is provided in exclude. To force characters to be "percent encoded" provide these as a QByteArray in include.

QByteArray text = "{a silly string?}";
QByteArray result = text.toPercentEncoding("{}", "s");
qDebug(result.constData()); // prints "{a %73illy %73tring%3F}"
See also
fromPercentEncoding(), QUrl::toPercentEncoding()
CFDataRef QByteArray::toRawCFData ( ) const

Constructs a CFData that uses the bytes of the QByteArray. The data is not copied.

The caller guarantees that the QByteArray will not be deleted or modified as long as this CFData object exists.

See also
toCFData(), fromRawCFData(), fromCFData(), fromRawData()
NSData * QByteArray::toRawNSData ( ) const

Constructs a NSData that uses the bytes of the QByteArray. The data is not copied.

The caller guarantees the QByteArray will not be deleted or modified as long as this NSData object exists.

See also
fromRawNSData(), fromNSData(), fromRawData(), toNSData()
short QByteArray::toShort ( bool *  ok = nullptr,
int  base = 10 
) const

Returns the byte array converted to a short using base, which is 10 by default and must be between 2 and 36, or 0. Returns 0 if the conversion fails.

If base is 0 it is determined using the following rules: (1) If the byte array begins with "0x" it is assumed to be hexadecimal (2) If it begins with "0" it is assumed to be octal, (3) otherwise it is assumed to be decimal.

If ok is not a nullptr then the status of the conversion is placed in ok. The value is false to indicate the conversion failed, otherwise ok is set to true.

Note
The conversion of the number is performed in the default C locale regardless of the user's locale.
See also
number()
uint QByteArray::toUInt ( bool *  ok = nullptr,
int  base = 10 
) const

Returns the byte array converted to an unsigned int using base, which is 10 by default and must be between 2 and 36, or 0. Returns 0 if the conversion fails.

If base is 0 it is determined using the following rules: (1) If the byte array begins with "0s" it is assumed to be hexadecimal (2) If it begins with "0" it is assumed to be octal, (3) otherwise it is assumed to be decimal.

If ok is not a nullptr, this method will set the value according to the success of the conversion. The value is false to indicate the conversion failed, otherwise ok is set to true.

Note
The conversion of the number is performed in the default C locale regardless of the user's locale.
See also
number()
ulong QByteArray::toULong ( bool *  ok = nullptr,
int  base = 10 
) const

Returns the byte array converted to an unsigned long int using base, which is 10 by default and must be between 2 and 36, or 0. Returns 0 if the conversion fails.

If base is 0 it is determined using the following rules: (1) If the byte array begins with "0x" it is assumed to be hexadecimal (2) If it begins with "0" it is assumed to be octal, (3) otherwise it is assumed to be decimal.

If ok is not a nullptr, this method will set the value according to the success of the conversion. The value is false to indicate the conversion failed, otherwise ok is set to true.

Note
The conversion of the number is performed in the default C locale regardless of the user's locale.
See also
number()
quint64 QByteArray::toULongLong ( bool *  ok = nullptr,
int  base = 10 
) const

Returns the byte array converted to an unsigned long long using base, which is 10 by default and must be between 2 and 36, or 0. Returns 0 if the conversion fails.

If base is 0 it is determined using the following rules: (1) If the byte array begins with "0x" it is assumed to be hexadecimal (2) If it begins with "0" it is assumed to be octal, (3) otherwise it is assumed to be decimal.

If ok is not a nullptr, this method will set the value according to the success of the conversion. The value is false to indicate the conversion failed, otherwise ok is set to true.

Note
The conversion of the number is performed in the default C locale regardless of the user's locale.
See also
number()
QByteArray QByteArray::toUpper ( ) const

Returns an uppercase copy of the byte array. The bytearray is interpreted as a Latin-1 encoded string.

QByteArray x("CopperSpice");
QByteArray y = x.toUpper(); // y == "COPPERSPICE"
See also
toLower(), 8-bit Character Comparisons
ushort QByteArray::toUShort ( bool *  ok = nullptr,
int  base = 10 
) const

Returns the byte array converted to an unsigned short using base, which is 10 by default and must be between 2 and 36, or 0. Returns 0 if the conversion fails.

If base is 0 it is determined using the following rules: (1) If the byte array begins with "0x" it is assumed to be hexadecimal (2) If it begins with "0" it is assumed to be octal, (3) otherwise it is assumed to be decimal.

If ok is not a nullptr, this method will set the value according to the success of the conversion. The value is false to indicate the conversion failed, otherwise ok is set to true.

Note
The conversion of the number is performed in the default C locale regardless of the user's locale.
See also
number()
QByteArray QByteArray::trimmed ( ) const

Returns a byte array that has whitespace removed from the start and the end.

Whitespace means any character for which the standard C++ isspace() method returns true. This includes the ASCII characters '\t', '\n', '\v', '\f', '\r', and ' '.

QByteArray value(" lots\t of\nwhitespace\r\n ");
value = value.trimmed(); // value == "lots\t of\nwhitespace";

Unlike simplified(), trimmed() leaves internal whitespace alone.

See also
simplified()
void QByteArray::truncate ( int  pos)

Truncates the byte array at index position pos. If pos is beyond the end of the array, nothing happens.

QByteArray value("Stockholm");
value.truncate(5); // value == "Stock"
See also
chop(), resize(), left()

Friends And Related Function Documentation

bool operator!= ( const char *  a1,
const QByteArray &  a2 
)
related

Returns true if string a1 is not equal to byte array a2, otherwise returns false.

bool operator!= ( const QByteArray &  a1,
const char *  a2 
)
related

Returns true if byte array a1 is not equal to string a2, otherwise returns false.

bool operator!= ( const QByteArray &  a1,
const QByteArray &  a2 
)
related

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

const QByteArray operator+ ( char  a1,
const QByteArray &  a2 
)
related

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

const QByteArray operator+ ( const char *  a1,
const QByteArray &  a2 
)
related

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

const QByteArray operator+ ( const QByteArray &  a1,
char  a2 
)
related

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

const QByteArray operator+ ( const QByteArray &  a1,
const char *  a2 
)
related

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

const QByteArray operator+ ( const QByteArray &  a1,
const QByteArray &  a2 
)
related

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

See also
operator+=()
bool operator< ( const char *  a1,
const QByteArray &  a2 
)
related

Returns true if string a1 is lexically less than byte array a2, otherwise returns false.

bool operator< ( const QByteArray &  a1,
const char *  a2 
)
related

Returns true if byte array a1 is lexically less than string a2, otherwise returns false.

bool operator< ( const QByteArray &  a1,
const QByteArray &  a2 
)
related

Returns true if byte array a1 is lexically less than byte array a2, otherwise returns false.

QDataStream & operator<< ( QDataStream stream,
const QByteArray &  byteArray 
)
related

Writes the given byteArray to the stream. Returns a reference to the stream.

Refer to Serializing Data Types for additional information.

bool operator<= ( const char *  data,
const QByteArray &  value 
)
related

Returns true if string data is lexically less than or equal to byte array value, otherwise returns false.

bool operator<= ( const QByteArray &  value,
const char *  data 
)
related

Returns true if byte array value1 is lexically less than or equal to string data, otherwise returns false.

bool operator<= ( const QByteArray &  value1,
const QByteArray &  value2 
)
related

Returns true if byte array value1 is lexically less than or equal to byte array value2, otherwise returns false.

bool operator== ( const char *  ch,
const QByteArray &  value 
)
related

Returns true if string ch is equal to byte array value, otherwise returns false.

bool operator== ( const QByteArray &  value1,
const char *  value2 
)
related

Returns true if byte array value1 is equal to string value2, otherwise returns false.

bool operator== ( const QByteArray &  value1,
const QByteArray &  value2 
)
related

Returns true if byte array value1 is equal to byte array value2, otherwise returns false.

bool operator> ( const char *  ch,
const QByteArray &  value 
)
related

Returns true if string ch is lexically greater than byte array value, otherwise returns false.

bool operator> ( const QByteArray &  value,
const char *  ch 
)
related

Returns true if byte array value is lexically greater than string ch, otherwise returns false.

bool operator> ( const QByteArray &  value1,
const QByteArray &  value2 
)
related

Returns true if byte array value1 is lexically greater than byte array value2, otherwise returns false.

bool operator>= ( const char *  ch,
const QByteArray &  value 
)
related

Returns true if string ch is lexically greater than or equal to byte array value, otherwise returns false.

bool operator>= ( const QByteArray &  value,
const char *  ch 
)
related

Returns true if byte array value is lexically greater than or equal to string ch, otherwise returns false.

bool operator>= ( const QByteArray &  value1,
const QByteArray &  value2 
)
related

Returns true if byte array value1 is lexically greater than or equal to byte array value2, otherwise returns false.

QDataStream & operator>> ( QDataStream stream,
QByteArray &  byteArray 
)
related

Reads from the stream into the given byteArray. Returns a reference to the stream.

Refer to Serializing Data Types for additional information.

quint16 qChecksum ( const char *  data,
uint  len 
)
related

Returns the CRC-16 checksum of the first len bytes of data. The checksum is independent of the byte order (endianness).

Note
This function is a 16-bit cache conserving (16 entry table) implementation of the CRC-16-CCITT algorithm.
QByteArray qCompress ( const QByteArray &  data,
int  compressionLevel = -1 
)
related

Compresses the data byte array and returns the compressed data in a new byte array.

The compressionLevel parameter specifies how much compression should be used. Valid values are between 0 and 9, with 9 corresponding to the greatest compression at the cost of using a slower algorithm. Smaller values then 9 provide successively less compression at slightly faster speeds. The value 0 corresponds to no compression at all. The default value is -1, which specifies zlib's default compression.

See also
qUncompress()
QByteArray qCompress ( const uchar data,
int  nbytes,
int  compressionLevel = -1 
)
related

Compresses the first nbytes of data and returns the compressed result in a new byte array. The compressionLevel parameter specifies how much compression should be used.

int qstrcmp ( const char *  str1,
const char *  str2 
)
related

Compares str1 and str2. Returns a negative value if str1 is less than str2, 0 if str1 is equal to str2, or a positive value if str1 is greater than str2.

Special case 1: Returns 0 if str1 and str2 are both nullptr.
Special case 2: Returns an arbitrary non-zero value if str1 is nullptr or str2 is nullptr (but not both).

See also
qstrncmp(), qstricmp(), qstrnicmp(), 8-bit Character Comparisons
char * qstrcpy ( char *  dst,
const char *  src 
)
related

Copies all the characters up to and including the null terminator from src into dst and returns a pointer to dst. If src is a nullptr, this function returns a nullptr.

This function assumes dst is large enough to hold the contents of src.

See also
qstrncpy()
char * qstrdup ( const char *  src)
related

Returns a duplicate string. Allocates space for a copy of src, copies it, and returns a pointer to the copy. If src is a nullptr, this function returns a nullptr.

Ownership is passed to the caller, so the returned string must be deleted using delete[].

int qstricmp ( const char *  str1,
const char *  str2 
)
related

Compares str1 and str2 ignoring the case of the characters. The encoding of the strings is assumed to be Latin-1.

Returns a negative value if str1 is less than <str2, 0 if str1 is equal to str2, or a positive value if str1 is greater than str2.

Special case 1: Returns 0 if str1 and str2 are both nullptr.
Special case 2: Returns a random non-zero value if str1 is nullptr or str2 is nullptr (but not both).

See also
qstrcmp(), qstrncmp(), qstrnicmp(), 8-bit Character Comparisons
uint qstrlen ( const char *  str)
related

Returns the number of characters which precede the terminating null character or 0 if str is a nullptr.

See also
qstrnlen()
int qstrncmp ( const char *  str1,
const char *  str2,
uint  len 
)
related

Compares at most len bytes of str1 and str2. Returns a negative value if str1 is less than str2, 0 if str1 is equal to str2, or a positive value if str1 is greater than str2.

Special case 1: Returns 0 if str1 and str2 are both nullptr.
Special case 2: Returns a random non-zero value if str1 is nullptr or str2 is nullptr (but not both).

See also
qstrcmp(), qstricmp(), qstrnicmp(), 8-bit Character Comparisons
char * qstrncpy ( char *  dst,
const char *  src,
uint  len 
)
related

Copies at most len bytes from src (stopping at len or the null terminator, whichever comes first) into dst and returns a pointer to dst. Guarantees that dst is null terminated. If src is a nullptr, this function returns a nullptr.

This function assumes that dst is at least len characters long.

See also
qstrcpy()
int qstrnicmp ( const char *  str1,
const char *  str2,
uint  len 
)
related

Compares at most len bytes of str1 and str2 ignoring the case of the characters. The encoding of the strings is assumed to be Latin-1. Returns a negative value if str1 is less than str2, 0 if str1 is equal to str2, or a positive value if str1 is greater than str2.

Special case 1: Returns 0 if str1 and str2 are both nullptr.
Special case 2: Returns a random non-zero value if str1 is nullptr or str2 is nullptr (but not both).

See also
qstrcmp(), qstrncmp(), qstricmp(), 8-bit Character Comparisons
uint qstrnlen ( const char *  str,
uint  maxlen 
)
related

Returns the number of characters which precede the null terminator but not more than maxlen. If str is a nullptr this method returns 0.

See also
qstrlen()
QByteArray qUncompress ( const QByteArray &  data)
related

Uncompresses the data byte array and returns a new byte array with the uncompressed data. Returns an empty QByteArray if the input data was corrupt.

Note
If you want to use this method to uncompress external data that was compressed using zlib, you first need to prepend a four byte header to the byte array containing the data. The header must contain the expected length (in bytes) of the uncompressed data, expressed as an unsigned, big-endian, 32-bit integer.
See also
qCompress()
QByteArray qUncompress ( const uchar data,
int  nbytes 
)
friend

Uncompresses the first nbytes of data and returns a new byte array with the uncompressed data.