QByteArray 類

The QByteArray 類提供字節數組。 更多...

頭: #include <QByteArray>
qmake: QT += core

注意: 此類的所有函數 可重入 .

公共類型

enum Base64Option { Base64Encoding, Base64UrlEncoding, KeepTrailingEquals, OmitTrailingEquals }
flags Base64Options
typedef const_iterator
typedef const_reverse_iterator
typedef iterator
typedef reverse_iterator

公共函數

QByteArray ()
QByteArray (const char * data , int size = -1)
QByteArray (int size , char ch )
QByteArray (const QByteArray & other )
QByteArray (QByteArray && other )
~QByteArray ()
QByteArray & append (const QByteArray & ba )
QByteArray & append (int count , char ch )
QByteArray & append (const char * str )
QByteArray & append (const char * str , int len )
QByteArray & append (char ch )
QByteArray & append (const QString & str )
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 (const QByteArray & ba ) const
bool contains (const char * str ) const
bool contains (char ch ) const
int count (const QByteArray & ba ) const
int count (const char * str ) const
int count (char ch ) const
int count () 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 (const QByteArray & ba ) const
bool endsWith (char ch ) const
bool endsWith (const char * str ) const
QByteArray & fill (char ch , int size = -1)
int indexOf (const QByteArray & ba , int from = 0) const
int indexOf (const char * str , int from = 0) const
int indexOf (char ch , int from = 0) const
int indexOf (const QString & str , int from = 0) const
QByteArray & insert (int i , const QByteArray & ba )
QByteArray & insert (int i , int count , char ch )
QByteArray & insert (int i , const char * str )
QByteArray & insert (int i , const char * str , int len )
QByteArray & insert (int i , char ch )
QByteArray & insert (int i , const QString & str )
bool isEmpty () const
bool isNull () const
int lastIndexOf (const QByteArray & ba , int from = -1) const
int lastIndexOf (const char * str , int from = -1) const
int lastIndexOf (char ch , int from = -1) const
int lastIndexOf (const QString & str , 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 & prepend (const QByteArray & ba )
QByteArray & prepend (int count , char ch )
QByteArray & prepend (const char * str )
QByteArray & prepend (const char * str , int len )
QByteArray & prepend (char ch )
void push_back (const QByteArray & other )
void push_back (const char * str )
void push_back (char ch )
void push_front (const QByteArray & other )
void push_front (const char * str )
void push_front (char ch )
reverse_iterator rbegin ()
const_reverse_iterator rbegin () const
QByteArray & remove (int pos , int len )
reverse_iterator rend ()
const_reverse_iterator rend () const
QByteArray repeated (int times ) const
QByteArray & replace (int pos , int len , const QByteArray & after )
QByteArray & replace (int pos , int len , const char * after , int alen )
QByteArray & replace (int pos , int len , const 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 , int bsize , const char * after , int asize )
QByteArray & replace (const QByteArray & before , const QByteArray & after )
QByteArray & replace (const QByteArray & before , const char * after )
QByteArray & replace (const char * before , const QByteArray & after )
QByteArray & replace (char before , char after )
QByteArray & replace (const QString & before , const char * after )
QByteArray & replace (char before , const QString & after )
QByteArray & replace (const QString & before , const QByteArray & after )
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 (int n , int base = 10)
QByteArray & setNum (ushort n , int base = 10)
QByteArray & setNum (short n , int base = 10)
QByteArray & setNum (uint n , int base = 10)
QByteArray & setNum (qlonglong n , int base = 10)
QByteArray & setNum (qulonglong n , int base = 10)
QByteArray & setNum (float n , char f = 'g', int prec = 6)
QByteArray & setNum (double n , char f = 'g', int prec = 6)
QByteArray & setRawData (const char * data , uint size )
QByteArray simplified () const
int size () const
QList<QByteArray> split (char sep ) const
void squeeze ()
bool startsWith (const QByteArray & ba ) const
bool startsWith (char ch ) const
bool startsWith (const char * str ) const
void swap (QByteArray & other )
QByteArray toBase64 () const
QByteArray toBase64 (Base64Options options ) const
CFDataRef toCFData () const
double toDouble (bool * ok = Q_NULLPTR) const
float toFloat (bool * ok = Q_NULLPTR) const
QByteArray toHex () const
QByteArray toHex (char separator ) const
int toInt (bool * ok = Q_NULLPTR, int base = 10) const
long toLong (bool * ok = Q_NULLPTR, int base = 10) const
qlonglong toLongLong (bool * ok = Q_NULLPTR, int base = 10) const
QByteArray toLower () const
NSData * toNSData () const
QByteArray toPercentEncoding (const QByteArray & exclude = QByteArray(), const QByteArray & 包括 = QByteArray(), char percent = '%') const
CFDataRef toRawCFData () const
NSData * toRawNSData () const
short toShort (bool * ok = Q_NULLPTR, int base = 10) const
std::string toStdString () const
uint toUInt (bool * ok = Q_NULLPTR, int base = 10) const
ulong toULong (bool * ok = Q_NULLPTR, int base = 10) const
qulonglong toULongLong (bool * ok = Q_NULLPTR, int base = 10) const
ushort toUShort (bool * ok = Q_NULLPTR, int base = 10) const
QByteArray toUpper () const
QByteArray trimmed () const
void truncate (int pos )
bool operator!= (const QString & str ) const
QByteArray & operator+= (const QByteArray & ba )
QByteArray & operator+= (const char * str )
QByteArray & operator+= (char ch )
QByteArray & operator+= (const QString & str )
bool operator< (const QString & str ) const
bool operator<= (const QString & str ) const
QByteArray & operator= (const QByteArray & other )
QByteArray & operator= (const char * str )
QByteArray & operator= (QByteArray && other )
bool operator== (const QString & str ) const
bool operator> (const QString & str ) const
bool operator>= (const QString & str ) const
QByteRef operator[] (int i )
char operator[] (uint i ) const
char operator[] (int i ) const
QByteRef operator[] (uint i )

靜態公共成員

QByteArray fromBase64 (const QByteArray & base64 )
QByteArray fromBase64 (const QByteArray & base64 , Base64Options options )
QByteArray fromCFData (CFDataRef data )
QByteArray fromHex (const QByteArray & hexEncoded )
QByteArray fromNSData (const NSData * data )
QByteArray fromPercentEncoding (const QByteArray & input , char percent = '%')
QByteArray fromRawCFData (CFDataRef data )
QByteArray fromRawData (const char * data , int size )
QByteArray fromRawNSData (const NSData * data )
QByteArray fromStdString (const std::string & str )
QByteArray number (int n , int base = 10)
QByteArray number (uint n , int base = 10)
QByteArray number (qlonglong n , int base = 10)
QByteArray number (qulonglong n , int base = 10)
QByteArray number (double n , char f = 'g', int prec = 6)
quint16 qChecksum (const char * data , uint len )
quint16 qChecksum (const char * data , uint len , Qt::ChecksumType standard )
QByteArray qCompress (const uchar * data , int nbytes , int compressionLevel = -1)
QByteArray qCompress (const QByteArray & data , int compressionLevel = -1)
QByteArray qUncompress (const QByteArray & data )
int qsnprintf (char * str , size_t n , const char * fmt , ... )
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 )
int qvsnprintf (char * str , size_t n , const char * fmt , va_list ap )
bool operator!= (const QByteArray & a1 , const QByteArray & a2 )
bool operator!= (const QByteArray & a1 , const char * a2 )
bool operator!= (const char * a1 , const QByteArray & a2 )
const QByteArray operator+ (const QByteArray & a1 , const QByteArray & a2 )
const QByteArray operator+ (const QByteArray & a1 , const char * a2 )
const QByteArray operator+ (const QByteArray & a1 , char a2 )
const QByteArray operator+ (const char * a1 , const QByteArray & a2 )
const QByteArray operator+ (char a1 , const QByteArray & a2 )
bool operator< (const QByteArray & a1 , const QByteArray & a2 )
bool operator< (const QByteArray & a1 , const char * a2 )
bool operator< (const char * a1 , const QByteArray & a2 )
QDataStream & operator<< (QDataStream & out , const QByteArray & ba )
bool operator<= (const QByteArray & a1 , const QByteArray & a2 )
bool operator<= (const QByteArray & a1 , const char * a2 )
bool operator<= (const char * a1 , const QByteArray & a2 )
bool operator== (const QByteArray & a1 , const QByteArray & a2 )
bool operator== (const QByteArray & a1 , const char * a2 )
bool operator== (const char * a1 , const QByteArray & a2 )
bool operator> (const QByteArray & a1 , const QByteArray & a2 )
bool operator> (const QByteArray & a1 , const char * a2 )
bool operator> (const char * a1 , const QByteArray & a2 )
bool operator>= (const QByteArray & a1 , const QByteArray & a2 )
bool operator>= (const QByteArray & a1 , const char * a2 )
bool operator>= (const char * a1 , const QByteArray & a2 )
QDataStream & operator>> (QDataStream & in , QByteArray & ba )

QByteArrayLiteral ( ba )
QT_NO_CAST_FROM_BYTEARRAY

詳細描述

The QByteArray 類提供字節數組。

QByteArray 可以用於存儲原生字節 (包括 \0) 和傳統 8 位以 \0 結尾的字符串。使用 QByteArray 更方便比使用 const char * 。在幕後,它始終確保數據之後緊跟 \0 終止符,並使用 隱式共享 (寫入時拷貝) 以縮減內存用量並避免不必要的數據拷貝。

除瞭 QByteArray ,Qt 還提供 QString 類以存儲字符串數據。對於大多數目的, QString 是您想要使用的類。它存儲 16 位 Unicode 字符,使之能在應用程序中輕鬆存儲非 ASCII 和非 Latin-1 字符。此外, QString 在 Qt API 中貫穿始終。這 2 種主要情況 QByteArray 是閤適的,當需要存儲原生二進製數據時,和當內存守恒臨界時 (如:采用 Qt for Embedded Linux)。

一種方式初始化 QByteArray 是隻需傳遞 const char * 到其構造函數。例如,以下代碼創建包含數據 Hello 大小為 5 的字節數組:

QByteArray ba("Hello");
					

盡管 size () is 5, the byte array also maintains an extra '\0' character at the end so that if a function is used that asks for a pointer to the underlying data (e.g. a call to data ()), the data pointed to is guaranteed to be '\0'-terminated.

QByteArray makes a deep copy of the const char * data, so you can modify it later without experiencing side effects. (If for performance reasons you don't want to take a deep copy of the character data, use QByteArray::fromRawData () instead.)

Another approach is to set the size of the array using resize () and to initialize the data byte per byte. QByteArray uses 0-based indexes, just like C++ arrays. To access the byte at a particular index position, you can use operator[](). On non-const byte arrays, operator[]() returns a reference to a byte that can be used on the left side of an assignment. For example:

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

For read-only access, an alternative syntax is to use at ():

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

at () can be faster than operator[](), because it never causes a 深拷貝 的齣現。

要每次提取多個字節,使用 left (), right (),或 mid ().

A QByteArray 可以嵌入 \0 字節。 size () function always returns the size of the whole array, including embedded '\0' bytes, but excluding the terminating '\0' added by QByteArray 。例如:

QByteArray ba1("ca\0r\0t");
ba1.size();                     // Returns 2.
ba1.constData();                // Returns "ca" with terminating \0.
QByteArray ba2("ca\0r\0t", 3);
ba2.size();                     // Returns 3.
ba2.constData();                // Returns "ca\0" with terminating \0.
QByteArray ba3("ca\0r\0t", 4);
ba3.size();                     // Returns 4.
ba3.constData();                // Returns "ca\0r" with terminating \0.
const char cart[] = {'c', 'a', '\0', 'r', '\0', 't'};
QByteArray ba4(QByteArray::fromRawData(cart, 6));
ba4.size();                     // Returns 6.
ba4.constData();                // Returns "ca\0r\0t" without terminating \0.
					

If you want to obtain the length of the data up to and excluding the first '\0' 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 ().

要獲得實際字符數據指針,調用 data () 或 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 QByteArray . It is also guaranteed that the data ends with a '\0' byte unless the QByteArray was created from a raw data . This '\0' byte is automatically provided by QByteArray and is not counted in size ().

QByteArray provides the following basic functions for modifying the byte data: append (), prepend (), insert (), replace (),和 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 () 和 remove () functions' 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 ,使用 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 ().

If you want to find all occurrences of a particular character or substring in a QByteArray ,使用 indexOf () 或 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 they find it; otherwise, they return -1. For example, here's a typical loop that finds all occurrences of a particular substring:

QByteArray ba("We must be <b>bold</b>, very <b>bold</b>");
int j = 0;
while ((j = ba.indexOf("<b>", j)) != -1) {
    cout << "Found <b> tag at index position " << j << endl;
    ++j;
}
					

If you simply want 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.

QByteArray s 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. QString::localeAwareCompare () is a better choice for sorting user-interface strings.

For historical reasons, 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 isn't 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
					

所有函數除瞭 isNull () treat null byte arrays the same as empty byte arrays. For example, data () returns a pointer to a '\0' character for a null byte array ( not a null pointer), and QByteArray () compares equal to QByteArray (""). We recommend that you always use isEmpty () and avoid isNull ().

區域設置注意事項

數字字符串轉換

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

8 位字符比較

QByteArray , the notion of uppercase and lowercase and of which character is greater than or less than another character is locale dependent. This affects functions that support a case insensitive option or that compare or lowercase or uppercase their arguments. Case insensitive operations and comparisons will be accurate if both strings contain only ASCII characters. (If $LC_CTYPE is set, most Unix systems do "the right thing".) Functions that this affects include contains (), indexOf (), lastIndexOf (), operator<(), operator<=(), operator>(), operator>=(), toLower () 和 toUpper ().

This issue does not apply to QString s since they represent characters using Unicode.

另請參閱 QString and QBitArray .

成員類型文檔編製

enum QByteArray:: Base64Option
flags QByteArray:: Base64Options

This enum contains the options available for encoding and decoding Base64. Base64 is defined by RFC 4648 , with the following options:

常量 描述
QByteArray::Base64Encoding 0 (default) The regular Base64 alphabet, called simply "base64"
QByteArray::Base64UrlEncoding 1 An alternate alphabet, called "base64url", which replaces two characters in the alphabet to be more friendly to URLs.
QByteArray::KeepTrailingEquals 0 (default) Keeps the trailing padding equal signs at the end of the encoded data, so the data is always a size multiple of four.
QByteArray::OmitTrailingEquals 2 Omits adding the padding equal signs at the end of the encoded data.

QByteArray::fromBase64 () ignores the KeepTrailingEquals and OmitTrailingEquals options and will not flag errors in case they are missing or if there are too many of them.

該枚舉在 Qt 5.2 引入或被修改。

The Base64Options type is a typedef for QFlags <Base64Option>. It stores an OR combination of Base64Option values.

typedef QByteArray:: const_iterator

此 typedef 提供 STL 樣式 const 迭代器為 QByteArray .

另請參閱 QByteArray::const_reverse_iterator and QByteArray::iterator .

typedef QByteArray:: const_reverse_iterator

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

該 typedef 在 Qt 5.6 引入。

另請參閱 QByteArray::reverse_iterator and QByteArray::const_iterator .

typedef QByteArray:: iterator

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

另請參閱 QByteArray::reverse_iterator and QByteArray::const_iterator .

typedef QByteArray:: reverse_iterator

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

該 typedef 在 Qt 5.6 引入。

另請參閱 QByteArray::const_reverse_iterator and QByteArray::iterator .

成員函數文檔編製

QByteArray:: QByteArray ()

構造空字節數組。

另請參閱 isEmpty ().

QByteArray:: QByteArray (const char * data , int size = -1)

構造字節數組包含前 size 字節的數組 data .

data 為 0,構造 null 字節數組。

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

QByteArray 製作字符串數據的深拷貝。

另請參閱 fromRawData ().

QByteArray:: QByteArray ( int size , char ch )

構造字節數組大小為 size 采用將每字節設為字符 ch .

另請參閱 fill ().

QByteArray:: QByteArray (const QByteArray & other )

構造副本為 other .

此操作花費 常量時間 ,因為 QByteArray is 隱式共享 . This makes returning a QByteArray from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), taking 綫性時間 .

另請參閱 operator= ().

QByteArray:: QByteArray ( QByteArray && other )

移動構造 QByteArray 實例,使之指嚮同一對象如 other 所指嚮的。

該函數在 Qt 5.2 引入。

QByteArray:: ~QByteArray ()

銷毀字節數組。

QByteArray &QByteArray:: append (const QByteArray & ba )

追加字節數組 ba 到此字節數組的末尾。

範例:

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

這如同 insert( size (), ba ).

注意: QByteArray 隱式共享 類。因此,若追加到空字節數組,那麼字節數組將僅僅共享的數據保持在 ba 。在這種情況下,不進行數據拷貝,花費 常量時間 。若共享實例被修改,它將被拷貝 (寫時拷貝),花費 綫性時間 .

若要追加到的字節數組非空,履行數據深拷貝,花費 綫性時間 .

This operation typically does not suffer from allocation overhead, because QByteArray preallocates extra space at the end of the data so that it may grow without reallocating for each append operation.

另請參閱 operator+= (), prepend (),和 insert ().

QByteArray &QByteArray:: append ( int count , char ch )

這是重載函數。

追加 count copies of character ch 到此字節數組,並返迴此字節數組的引用。

count 為負 (或 0),什麼都不會追加到字節數組。

該函數在 Qt 5.7 引入。

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

這是重載函數。

追加字符串 str 到此字節數組。

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

此函數重載 append ().

追加第一 len characters of the string str 到此字節數組,並返迴此字節數組的引用。

len is negative, the length of the string will be determined automatically using qstrlen ()。若 len 為 0 或 str is null, nothing is appended to the byte array. Ensure that len is not longer than str .

QByteArray &QByteArray:: append ( char ch )

這是重載函數。

追加字符 ch 到此字節數組。

QByteArray &QByteArray:: append (const QString & str )

這是重載函數。

追加字符串 str to this byte array. The Unicode data is converted into 8-bit characters using QString::toUtf8 ().

可以禁用此函數通過定義 QT_NO_CAST_TO_ASCII 當編譯應用程序時。那麼需要調用 QString::toUtf8 () (或 QString::toLatin1 () 或 QString::toLocal8Bit ()) explicitly if you want to convert the data to const char * .

char QByteArray:: at ( int i ) const

返迴字符在索引位置 i 在字節數組中。

i 必須是字節數組中的有效索引位置 (即 0 <= i < size ()).

另請參閱 operator[] ().

iterator QByteArray:: begin ()

返迴 STL 樣式迭代器 pointing to the first character in the byte-array.

另請參閱 constBegin () 和 end ().

const_iterator QByteArray:: begin () const

此函數重載 begin ().

int QByteArray:: capacity () const

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

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

另請參閱 reserve () 和 squeeze ().

const_iterator QByteArray:: cbegin () const

返迴常量 STL 樣式迭代器 pointing to the first character in the byte-array.

該函數在 Qt 5.0 引入。

另請參閱 begin () 和 cend ().

const_iterator QByteArray:: cend () const

返迴常量 STL 樣式迭代器 pointing to the imaginary character after the last character in the list.

該函數在 Qt 5.0 引入。

另請參閱 cbegin () 和 end ().

void QByteArray:: chop ( int n )

移除 n bytes from the end of the byte array.

n 大於 size (), the result is an empty byte array.

範例:

QByteArray ba("STARTTLS\r\n");
ba.chop(2);                 // ba == "STARTTLS"
					

另請參閱 truncate (), resize (),和 left ().

void QByteArray:: clear ()

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

另請參閱 resize () 和 isNull ().

const_iterator QByteArray:: constBegin () const

返迴常量 STL 樣式迭代器 pointing to the first character in the byte-array.

另請參閱 begin () 和 constEnd ().

const char *QByteArray:: 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 '\0'-terminated unless the QByteArray object was created from raw data. The pointer remains valid as long as the byte array isn't reallocated or destroyed.

此函數主要用於將字節數組傳遞給的函數接受 const char * .

注意: QByteArray 可以存儲任何字節值 (包括 \0),但大多數函數需要 char * 自變量,假定數據在遇到首個 \0 時結束。

另請參閱 data (), operator[] (),和 fromRawData ().

const_iterator QByteArray:: constEnd () const

返迴常量 STL 樣式迭代器 pointing to the imaginary character after the last character in the list.

另請參閱 constBegin () 和 end ().

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

返迴 true if the byte array contains an occurrence of the byte array ba ;否則返迴 false .

另請參閱 indexOf () 和 count ().

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

這是重載函數。

返迴 true if the byte array contains the string str ;否則返迴 false .

bool QByteArray:: contains ( char ch ) const

這是重載函數。

返迴 true if the byte array contains the character ch ;否則返迴 false .

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

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

另請參閱 contains () 和 indexOf ().

int QByteArray:: count (const char * str ) const

這是重載函數。

Returns the number of (potentially overlapping) occurrences of string str 在字節數組中。

int QByteArray:: count ( char ch ) const

這是重載函數。

返迴齣現數對於字符 ch 在字節數組中。

另請參閱 contains () 和 indexOf ().

int QByteArray:: count () const

這是重載函數。

如同 size ().

const_reverse_iterator QByteArray:: crbegin () const

返迴常量 STL-style reverse iterator pointing to the first character in the byte-array, in reverse order.

該函數在 Qt 5.6 引入。

另請參閱 begin (), rbegin (),和 rend ().

const_reverse_iterator QByteArray:: crend () const

返迴常量 STL-style reverse iterator pointing to one past the last character in the byte-array, in reverse order.

該函數在 Qt 5.6 引入。

另請參閱 end (), rend (),和 rbegin ().

char *QByteArray:: 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 '\0'-terminated, i.e. the number of bytes in the returned character string is size () + 1 for the '\0' terminator.

範例:

QByteArray ba("Hello world");
char *data = ba.data();
while (*data) {
    cout << "[" << *data << "]" << endl;
    ++data;
}
					

The pointer remains valid as long as the byte array isn't reallocated or destroyed. For read-only access, constData () 更快,因為它從不導緻 深拷貝 的齣現。

此函數主要用於將字節數組傳遞給的函數接受 const char * .

以下範例製作由 data() 返迴的 char* 副本,但會破壞堆並導緻崩潰,因為它沒有為末尾 \0 分配字節:

QString tmp = "test";
QByteArray text = tmp.toLocal8Bit();
char *data = new char[text.size()];
strcpy(data, text.data());
delete [] data;
					

這個分配瞭正確的空間數量:

QString tmp = "test";
QByteArray text = tmp.toLocal8Bit();
char *data = new char[text.size() + 1];
strcpy(data, text.data());
delete [] data;
					

注意: QByteArray 可以存儲任何字節值 (包括 \0),但大多數函數需要 char * 自變量,假定數據在遇到首個 \0 時結束。

另請參閱 constData () 和 operator[] ().

const char *QByteArray:: data () const

這是重載函數。

iterator QByteArray:: end ()

返迴 STL 樣式迭代器 pointing to the imaginary character after the last character in the byte-array.

另請參閱 begin () 和 constEnd ().

const_iterator QByteArray:: end () const

此函數重載 end ().

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

返迴 true if this byte array ends with byte array ba ;否則返迴 false .

範例:

QByteArray url("http://qt-project.org/doc/qt-5.0/qtdoc/index.html");
if (url.endsWith(".html"))
    ...
					

另請參閱 startsWith () 和 right ().

bool QByteArray:: endsWith ( char ch ) const

這是重載函數。

返迴 true if this byte array ends with character ch ;否則返迴 false .

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

這是重載函數。

返迴 true if this byte array ends with string str ;否則返迴 false .

QByteArray &QByteArray:: fill ( char ch , int size = -1)

Sets every byte in the byte array to character ch 。若 size is different from -1 (the default), the byte array is resized to size size 事先。

範例:

QByteArray ba("Istambul");
ba.fill('o');
// ba == "oooooooo"
ba.fill('X', 2);
// ba == "XX"
					

另請參閱 resize ().

[static] QByteArray QByteArray:: fromBase64 (const QByteArray & base64 )

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

例如:

QByteArray text = QByteArray::fromBase64("UXQgaXMgZ3JlYXQh");
text.data();            // returns "Qt is great!"
					

The algorithm used to decode Base64-encoded data is defined in RFC 4648 .

另請參閱 toBase64 ().

[static] QByteArray QByteArray:: fromBase64 (const QByteArray & base64 , Base64Options options )

這是重載函數。

Returns a decoded copy of the Base64 array base64 , using the alphabet defined by options . Input is not checked for validity; invalid characters in the input are skipped, enabling the decoding process to continue with subsequent characters.

例如:

QByteArray::fromBase64("PHA+SGVsbG8/PC9wPg==", QByteArray::Base64Encoding); // returns "<p>Hello?</p>"
QByteArray::fromBase64("PHA-SGVsbG8_PC9wPg==", QByteArray::Base64UrlEncoding); // returns "<p>Hello?</p>"
					

The algorithm used to decode Base64-encoded data is defined in RFC 4648 .

該函數在 Qt 5.2 引入。

另請參閱 toBase64 ().

[static] QByteArray QByteArray:: fromCFData ( CFDataRef data )

構造新的 QByteArray containing a copy of the CFData data .

該函數在 Qt 5.3 引入。

另請參閱 fromRawCFData (), fromRawData (), toRawCFData (),和 toCFData ().

[static] QByteArray QByteArray:: fromHex (const QByteArray & 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.

例如:

QByteArray text = QByteArray::fromHex("517420697320677265617421");
text.data();            // returns "Qt is great!"
					

另請參閱 toHex ().

[static] QByteArray QByteArray:: fromNSData (const NSData * data )

構造新的 QByteArray containing a copy of the NSData data .

該函數在 Qt 5.3 引入。

另請參閱 fromRawNSData (), fromRawData (), toNSData (),和 toRawNSData ().

[static] QByteArray QByteArray:: fromPercentEncoding (const QByteArray & input , char percent = '%')

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

例如:

QByteArray text = QByteArray::fromPercentEncoding("Qt%20is%20great%33");
text.data();            // returns "Qt is great!"
					

注意: 給定無效輸入 (譬如:包含 %G5 序列的字符串,不是有效十六進製數字),輸齣也將無效。例如:%G5 序列可以解碼為 W。

該函數在 Qt 4.4 引入。

另請參閱 toPercentEncoding () 和 QUrl::fromPercentEncoding ().

[static] QByteArray QByteArray:: fromRawCFData ( CFDataRef data )

構造 QByteArray that uses the bytes of the CFData data .

The data 的字節不拷貝。

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

該函數在 Qt 5.3 引入。

另請參閱 fromCFData (), fromRawData (), toRawCFData (),和 toCFData ().

[static] QByteArray QByteArray:: fromRawData (const char * data , int size )

構造 QByteArray that uses the first size bytes of the data array. The bytes are not copied. The QByteArray will contain the data pointer. The caller guarantees that data will not be deleted or modified as long as this QByteArray and any copies of it exist that have not been modified. In other words, because QByteArray 隱式共享 class and the instance returned by this function contains the data pointer, the caller must not delete data or modify it directly as long as the returned QByteArray and any copies exist. However, QByteArray 未擁有所有權對於 data , so the QByteArray destructor will never delete the raw data , even when the last QByteArray referring to data 被銷毀。

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 .

Here is an 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);
...
					

警告: A byte array created with fromRawData() is not null-terminated, unless the raw data contains a 0 character at position size . While that does not matter for QDataStream or functions like indexOf (), passing the byte array to a function accepting a const char * expected to be '\0'-terminated will fail.

另請參閱 setRawData (), data (),和 constData ().

[static] QByteArray QByteArray:: fromRawNSData (const NSData * data )

構造 QByteArray that uses the bytes of the NSData data .

The data 的字節不拷貝。

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

該函數在 Qt 5.3 引入。

另請參閱 fromNSData (), fromRawData (), toRawNSData (),和 toNSData ().

[static] QByteArray QByteArray:: fromStdString (const std::string & str )

返迴副本為 str string as a QByteArray .

該函數在 Qt 5.4 引入。

另請參閱 toStdString () 和 QString::fromStdString ().

int QByteArray:: indexOf (const QByteArray & 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 index position from . Returns -1 if ba 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
					

另請參閱 lastIndexOf (), contains (),和 count ().

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 str could not be found.

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 ba("ABCBA");
ba.indexOf("B");            // returns 1
ba.indexOf("B", 1);         // returns 1
ba.indexOf("B", 2);         // returns 3
ba.indexOf("X");            // returns -1
					

另請參閱 lastIndexOf () 和 contains ().

int QByteArray:: indexOf (const QString & 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 str could not be found.

The Unicode data is converted into 8-bit characters using QString::toUtf8 ().

可以禁用此函數通過定義 QT_NO_CAST_TO_ASCII 當編譯應用程序時。那麼需要調用 QString::toUtf8 () (或 QString::toLatin1 () 或 QString::toLocal8Bit ()) explicitly if you want to convert the data to const char * .

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

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

範例:

QByteArray ba("Meal");
ba.insert(1, QByteArray("ontr"));
// ba == "Montreal"
					

另請參閱 append (), prepend (), replace (),和 remove ().

QByteArray &QByteArray:: insert ( int i , int count , char ch )

這是重載函數。

插入 count copies of character ch at index position i 在字節數組中。

i 大於 size (), the array is first extended using resize ().

該函數在 Qt 5.7 引入。

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

這是重載函數。

插入字符串 str 在位置 i 在字節數組中。

i 大於 size (), the array is first extended using resize ().

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

這是重載函數。

插入 len bytes of the string str 在位置 i 在字節數組中。

i 大於 size (), the array is first extended using resize ().

該函數在 Qt 4.6 引入。

QByteArray &QByteArray:: insert ( int i , char ch )

這是重載函數。

Inserts character ch at index position i in the byte array. If i 大於 size (), the array is first extended using resize ().

QByteArray &QByteArray:: insert ( int i , const QString & str )

這是重載函數。

插入字符串 str at index position i in the byte array. The Unicode data is converted into 8-bit characters using QString::toUtf8 ().

i 大於 size (), the array is first extended using resize ().

可以禁用此函數通過定義 QT_NO_CAST_TO_ASCII 當編譯應用程序時。那麼需要調用 QString::toUtf8 () (或 QString::toLatin1 () 或 QString::toLocal8Bit ()) explicitly if you want to convert the data to const char * .

bool QByteArray:: isEmpty () const

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

範例:

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

另請參閱 size ().

bool QByteArray:: isNull () const

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

範例:

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

Qt 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 ().

另請參閱 isEmpty ().

int QByteArray:: lastIndexOf (const QByteArray & 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 index position from 。若 from is -1 (the default), the search starts at the last byte. Returns -1 if ba 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
					

另請參閱 indexOf (), contains (),和 count ().

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 。若 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 ( 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 。若 from is -1 (the default), the search starts at the last ( size () - 1) byte. Returns -1 if ch could not be found.

範例:

QByteArray ba("ABCBA");
ba.lastIndexOf("B");        // returns 3
ba.lastIndexOf("B", 3);     // returns 3
ba.lastIndexOf("B", 2);     // returns 1
ba.lastIndexOf("X");        // returns -1
					

另請參閱 indexOf () 和 contains ().

int QByteArray:: lastIndexOf (const QString & 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 。若 from is -1 (the default), the search starts at the last ( size () - 1) byte. Returns -1 if str could not be found.

The Unicode data is converted into 8-bit characters using QString::toUtf8 ().

可以禁用此函數通過定義 QT_NO_CAST_TO_ASCII 當編譯應用程序時。那麼需要調用 QString::toUtf8 () (或 QString::toLatin1 () 或 QString::toLocal8Bit ()) explicitly if you want to convert the data to const char * .

QByteArray QByteArray:: left ( int len ) const

返迴的字節數組包含最左 len 字節對於此字節數組。

返迴整個字節數組若 len 大於 size ().

範例:

QByteArray x("Pineapple");
QByteArray y = x.left(4);
// y == "Pine"
					

另請參閱 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.

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.

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..."
					

另請參閱 rightJustified ().

int QByteArray:: length () const

如同 size ().

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

返迴的字節數組包含 len 字節從此字節數組,起始於位置 pos .

len 為 -1 (默認),或 pos + len >= size (), 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"
					

另請參閱 left () 和 right ().

[static] QByteArray QByteArray:: number ( int n , int base = 10)

Returns a byte array containing the string equivalent of the number n to base 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"
					

注意: The format of the number is not localized; the default C locale is used irrespective of the user's locale.

另請參閱 setNum () 和 toInt ().

[static] QByteArray QByteArray:: number ( uint n , int base = 10)

這是重載函數。

另請參閱 toUInt ().

[static] QByteArray QByteArray:: number ( qlonglong n , int base = 10)

這是重載函數。

另請參閱 toLongLong ().

[static] QByteArray QByteArray:: number ( qulonglong n , int base = 10)

這是重載函數。

另請參閱 toULongLong ().

[static] QByteArray QByteArray:: number ( double n , char f = 'g', int prec = 6)

這是重載函數。

Returns a byte array that contains the printed value of n , formatted in format f with precision prec .

自變量 n is formatted according to the f format specified, which is g by default, and can be any of the following:

格式 含義
e format as [-]9.9e[+|-]999
E format as [-]9.9E[+|-]999
f format as [-]9.9
g 使用 e or f format, whichever is the most concise
G 使用 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 ba = QByteArray::number(12.3456, 'E', 3);
// ba == 1.235E+01
					

注意: The format of the number is not localized; the default C locale is used irrespective of the user's locale.

另請參閱 toDouble ().

QByteArray &QByteArray:: prepend (const QByteArray & ba )

前置字節數組 ba 到此字節數組,並返迴此字節數組的引用。

範例:

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

這如同 insert(0, ba ).

注意: QByteArray 隱式共享 class. Consequently, if you prepend to an empty byte array, then the byte array will just share the data held in ba 。在這種情況下,不進行數據拷貝,花費 常量時間 。若共享實例被修改,它將被拷貝 (寫時拷貝),花費 綫性時間 .

If the byte array being prepended to is not empty, a deep copy of the data is performed, taking 綫性時間 .

另請參閱 append () 和 insert ().

QByteArray &QByteArray:: prepend ( int count , char ch )

這是重載函數。

前置 count copies of character ch 到此字節數組。

該函數在 Qt 5.7 引入。

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

這是重載函數。

前置字符串 str 到此字節數組。

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

這是重載函數。

前置 len bytes of the string str 到此字節數組。

該函數在 Qt 4.6 引入。

QByteArray &QByteArray:: prepend ( char ch )

這是重載函數。

前置字符 ch 到此字節數組。

void QByteArray:: push_back (const QByteArray & other )

This function is provided for STL compatibility. It is equivalent to append( other ).

void QByteArray:: push_back (const char * str )

這是重載函數。

如同 append( str ).

void QByteArray:: push_back ( char ch )

這是重載函數。

如同 append( ch ).

void QByteArray:: push_front (const QByteArray & other )

This function is provided for STL compatibility. It is equivalent to prepend( other ).

void QByteArray:: push_front (const char * str )

這是重載函數。

如同 prepend( str ).

void QByteArray:: push_front ( char ch )

這是重載函數。

如同 prepend( ch ).

reverse_iterator QByteArray:: rbegin ()

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

該函數在 Qt 5.6 引入。

另請參閱 begin (), crbegin (),和 rend ().

const_reverse_iterator QByteArray:: rbegin () const

這是重載函數。

該函數在 Qt 5.6 引入。

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

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

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 ba("Montreal");
ba.remove(1, 4);
// ba == "Meal"
					

另請參閱 insert () 和 replace ().

reverse_iterator QByteArray:: rend ()

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

該函數在 Qt 5.6 引入。

另請參閱 end (), crend (),和 rbegin ().

const_reverse_iterator QByteArray:: rend () const

這是重載函數。

該函數在 Qt 5.6 引入。

QByteArray QByteArray:: repeated ( int times ) const

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

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

範例:

QByteArray ba("ab");
ba.repeated(4);             // returns "abababab"
					

該函數在 Qt 4.5 引入。

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

替換 len bytes from index position pos with the byte array after , and returns a reference to this byte array.

範例:

QByteArray x("Say yes!");
QByteArray y("no");
x.replace(4, 3, y);
// x == "Say no!"
					

另請參閱 insert () 和 remove ().

QByteArray &QByteArray:: replace ( int pos , int len , const char * after , int alen )

這是重載函數。

替換 len bytes from index position pos with alen bytes from the string after . after is allowed to have '\0' characters.

該函數在 Qt 4.7 引入。

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

這是重載函數。

替換 len bytes from index position pos with the zero terminated string after .

注意:這可以改變字節數組的長度。

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

這是重載函數。

Replaces every occurrence of the character before 采用字符串 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 )

這是重載函數。

Replaces every occurrence of the string before 采用字符串 after .

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

這是重載函數。

Replaces every occurrence of the string before 采用字符串 after . Since the sizes of the strings are given by bsize and asize , they may contain zero characters and do not need to be zero-terminated.

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

這是重載函數。

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

範例:

QByteArray ba("colour behaviour flavour neighbour");
ba.replace(QByteArray("ou"), QByteArray("o"));
// ba == "color behavior flavor neighbor"
					

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

這是重載函數。

Replaces every occurrence of the byte array before 采用字符串 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 ( char before , char after )

這是重載函數。

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

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

這是重載函數。

Replaces every occurrence of the string before 采用字符串 after .

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

這是重載函數。

Replaces every occurrence of the character before 采用字符串 after . The Unicode data is converted into 8-bit characters using QString::toUtf8 ().

可以禁用此函數通過定義 QT_NO_CAST_TO_ASCII 當編譯應用程序時。那麼需要調用 QString::toUtf8 () (或 QString::toLatin1 () 或 QString::toLocal8Bit ()) explicitly if you want to convert the data to const char * .

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

這是重載函數。

Replaces every occurrence of the string before with the byte array after . The Unicode data is converted into 8-bit characters using QString::toUtf8 ().

可以禁用此函數通過定義 QT_NO_CAST_TO_ASCII 當編譯應用程序時。那麼需要調用 QString::toUtf8 () (或 QString::toLatin1 () 或 QString::toLocal8Bit ()) explicitly if you want to convert the data to const char * .

void QByteArray:: reserve ( int size )

試圖分配內存為至少 size bytes. If you know in advance how large the byte array will be, you can call this function, 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 function is to provide a means of fine tuning QByteArray 's memory usage. In general, you will rarely ever need to call this function. If you want to change the size of the byte array, call resize ().

另請參閱 squeeze () 和 capacity ().

void QByteArray:: resize ( int size )

把字節數組的尺寸設為 size 字節。

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.

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

另請參閱 size () 和 truncate ().

返迴的字節數組包含最右 len 字節對於此字節數組。

返迴整個字節數組若 len 大於 size ().

範例:

QByteArray x("Pineapple");
QByteArray y = x.right(5);
// y == "apple"
					

另請參閱 endsWith (), left (),和 mid ().

QByteArray QByteArray:: rightJustified ( int width , char fill = ' ', bool truncate = false) const

Returns a byte array of size width 包含 fill character followed by this byte array.

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.

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"
					

另請參閱 leftJustified ().

QByteArray &QByteArray:: setNum ( int n , int base = 10)

Sets the byte array to the printed value of n in base base (10 by default) and returns a reference to the byte array. The base can be any value between 2 and 36. For bases other than 10, n is treated as an unsigned integer.

範例:

QByteArray ba;
int n = 63;
ba.setNum(n);           // ba == "63"
ba.setNum(n, 16);       // ba == "3f"
					

注意: The format of the number is not localized; the default C locale is used irrespective of the user's locale.

另請參閱 number () 和 toInt ().

QByteArray &QByteArray:: setNum ( ushort n , int base = 10)

這是重載函數。

另請參閱 toUShort ().

QByteArray &QByteArray:: setNum ( short n , int base = 10)

這是重載函數。

另請參閱 toShort ().

QByteArray &QByteArray:: setNum ( uint n , int base = 10)

這是重載函數。

另請參閱 toUInt ().

QByteArray &QByteArray:: setNum ( qlonglong n , int base = 10)

這是重載函數。

另請參閱 toLongLong ().

QByteArray &QByteArray:: setNum ( qulonglong n , int base = 10)

這是重載函數。

另請參閱 toULongLong ().

QByteArray &QByteArray:: setNum ( float 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 of the number is not localized; the default C locale is used irrespective of the user's locale.

另請參閱 toFloat ().

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.

格式 f 可以是下列任一:

格式 含義
e format as [-]9.9e[+|-]999
E format as [-]9.9E[+|-]999
f format as [-]9.9
g 使用 e or f format, whichever is the most concise
G 使用 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).

注意: The format of the number is not localized; the default C locale is used irrespective of the user's locale.

另請參閱 toDouble ().

QByteArray &QByteArray:: setRawData (const char * data , uint size )

Resets the QByteArray to use the first size bytes of the data array. The bytes are not copied. The QByteArray will contain the data pointer. The caller guarantees that data will not be deleted or modified as long as this QByteArray and any copies of it exist that have not been modified.

This function can be used instead of fromRawData () to re-use existing QByteArray objects to save memory re-allocations.

該函數在 Qt 4.7 引入。

另請參閱 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() 函數返迴 true in the C locale. This includes the ASCII isspace() function returns true in the C locale. This includes the ASCII characters '\t', '\n', '\v', '\f', '\r', and ' '.

範例:

QByteArray ba("  lots\t of\nwhitespace\r\n ");
ba = ba.simplified();
// ba == "lots of whitespace";
					

另請參閱 trimmed ().

int QByteArray:: size () const

返迴此字節數組的字節數。

The last byte in the byte array is at position size() - 1. In addition, QByteArray ensures that the byte at position size() is always '\0', so that you can use the return value of data () 和 constData () as arguments to functions that expect '\0'-terminated strings. If the QByteArray object was created from a raw data that didn't include the trailing null-termination character then QByteArray doesn't add it automaticall unless the 深拷貝 被創建。

範例:

QByteArray ba("Hello");
int n = ba.size();          // n == 5
ba.data()[0];               // returns 'H'
ba.data()[4];               // returns 'o'
ba.data()[5];               // returns '\0'
					

另請參閱 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 ()

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

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

另請參閱 reserve () 和 capacity ().

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

返迴 true 若此字節數組開頭采用字節數組 ba ;否則返迴 false .

範例:

QByteArray url("ftp://ftp.qt-project.org/");
if (url.startsWith("ftp:"))
    ...
					

另請參閱 endsWith () 和 left ().

bool QByteArray:: startsWith ( char ch ) const

這是重載函數。

返迴 true if this byte array starts with character ch ;否則返迴 false .

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

這是重載函數。

返迴 true if this byte array starts with string str ;否則返迴 false .

void QByteArray:: swap ( QByteArray & other )

交換字節數組 other with this byte array. This operation is very fast and never fails.

該函數在 Qt 4.8 引入。

QByteArray QByteArray:: toBase64 () const

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

QByteArray text("Qt is great!");
text.toBase64();        // returns "UXQgaXMgZ3JlYXQh"
					

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

另請參閱 fromBase64 ().

QByteArray QByteArray:: toBase64 ( Base64Options options ) const

這是重載函數。

返迴字節數組的副本,編碼使用選項 options .

QByteArray text("<p>Hello?</p>");
text.toBase64(QByteArray::Base64Encoding | QByteArray::OmitTrailingEquals);      // returns "PHA+SGVsbG8/PC9wPg"
text.toBase64(QByteArray::Base64Encoding);                                       // returns "PHA+SGVsbG8/PC9wPg=="
text.toBase64(QByteArray::Base64UrlEncoding);                                    // returns "PHA-SGVsbG8_PC9wPg=="
text.toBase64(QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals);   // returns "PHA-SGVsbG8_PC9wPg"
					

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

該函數在 Qt 5.2 引入。

另請參閱 fromBase64 ().

CFDataRef QByteArray:: toCFData () const

創建 CFData 從 QByteArray . The caller owns the CFData object and is responsible for releasing it.

該函數在 Qt 5.3 引入。

另請參閱 toRawCFData (), fromCFData (), fromRawCFData (),和 fromRawData ().

double QByteArray:: toDouble ( bool * ok = Q_NULLPTR) const

返迴字節數組被轉換成 double 值。

Returns 0.0 if the conversion fails.

ok is not 0: if a conversion error occurs, * ok is set to false; otherwise * ok 被設為 true。

QByteArray string("1234.56");
double a = string.toDouble();   // a == 1234.56
					

注意: The conversion of the number is performed in the default C locale, irrespective of the user's locale.

另請參閱 number ().

float QByteArray:: toFloat ( bool * ok = Q_NULLPTR) const

返迴字節數組被轉換成 float 值。

Returns 0.0 if the conversion fails.

ok is not 0: if a conversion error occurs, * ok is set to false; otherwise * ok 被設為 true。

注意: The conversion of the number is performed in the default C locale, irrespective of the user's locale.

另請參閱 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.

另請參閱 fromHex ().

QByteArray QByteArray:: toHex ( char separator ) const

這是重載函數。

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

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

範例:

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

該函數在 Qt 5.9 引入。

另請參閱 fromHex ().

int QByteArray:: toInt ( bool * ok = Q_NULLPTR, int base = 10) const

返迴字節數組被轉換成 int 使用基 base , which is 10 by default and must be between 2 and 36, or 0.

base is 0, the base is determined automatically using the following rules: If the byte array begins with "0x", it is assumed to be hexadecimal; if it begins with "0", it is assumed to be octal; otherwise it is assumed to be decimal.

返迴 0,若轉換失敗。

ok is not 0: if a conversion error occurs, * ok is set to false; otherwise * ok 被設為 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
					

注意: The conversion of the number is performed in the default C locale, irrespective of the user's locale.

另請參閱 number ().

long QByteArray:: toLong ( bool * ok = Q_NULLPTR, int base = 10) const

返迴字節數組被轉換成 long int using base base , which is 10 by default and must be between 2 and 36, or 0.

base is 0, the base is determined automatically using the following rules: If the byte array begins with "0x", it is assumed to be hexadecimal; if it begins with "0", it is assumed to be octal; otherwise it is assumed to be decimal.

返迴 0,若轉換失敗。

ok is not 0: if a conversion error occurs, * ok is set to false; otherwise * ok 被設為 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
					

注意: The conversion of the number is performed in the default C locale, irrespective of the user's locale.

該函數在 Qt 4.1 引入。

另請參閱 number ().

qlonglong QByteArray:: toLongLong ( bool * ok = Q_NULLPTR, int base = 10) const

返迴字節數組被轉換成 long long 使用基 base , which is 10 by default and must be between 2 and 36, or 0.

base is 0, the base is determined automatically using the following rules: If the byte array begins with "0x", it is assumed to be hexadecimal; if it begins with "0", it is assumed to be octal; otherwise it is assumed to be decimal.

返迴 0,若轉換失敗。

ok is not 0: if a conversion error occurs, * ok is set to false; otherwise * ok 被設為 true。

注意: The conversion of the number is performed in the default C locale, irrespective of the user's locale.

另請參閱 number ().

QByteArray QByteArray:: toLower () const

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

範例:

QByteArray x("Qt by THE QT COMPANY");
QByteArray y = x.toLower();
// y == "qt by the qt company"
					

另請參閱 toUpper () 和 8 位字符比較 .

NSData *QByteArray:: toNSData () const

創建 NSData 從 QByteArray . The NSData object is autoreleased.

該函數在 Qt 5.3 引入。

另請參閱 fromNSData (), fromRawNSData (), fromRawData (),和 toRawNSData ().

QByteArray QByteArray:: toPercentEncoding (const QByteArray & exclude = QByteArray(), const QByteArray & 包括 = QByteArray(), char percent = '%') const

Returns a URI/URL-style percent-encoded copy of this byte array. The percent parameter allows you to override the default '%' character for another.

By default, this function will encode all characters that are not one of the following:

ALPHA ("a" to "z" and "A" to "Z") / DIGIT (0 to 9) / "-" / "." / "_" / "~"

To prevent characters from being encoded pass them to exclude . To force characters to be encoded pass them to 包括 percent character is always encoded.

範例:

QByteArray text = "{a fishy string?}";
QByteArray ba = text.toPercentEncoding("{}", "s");
qDebug(ba.constData());
// prints "{a fi%73hy %73tring%3F}"
					

The hex encoding uses the numbers 0-9 and the uppercase letters A-F.

該函數在 Qt 4.4 引入。

另請參閱 fromPercentEncoding () 和 QUrl::toPercentEncoding ().

CFDataRef QByteArray:: toRawCFData () const

Constructs a CFData that uses the bytes of the QByteArray .

The QByteArray 的字節不拷貝。

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

該函數在 Qt 5.3 引入。

另請參閱 toCFData (), fromRawCFData (), fromCFData (),和 fromRawData ().

NSData *QByteArray:: toRawNSData () const

Constructs a NSData that uses the bytes of the QByteArray .

The QByteArray 的字節不拷貝。

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

該函數在 Qt 5.3 引入。

另請參閱 fromRawNSData (), fromNSData (), fromRawData (),和 toNSData ().

short QByteArray:: toShort ( bool * ok = Q_NULLPTR, int base = 10) const

返迴字節數組被轉換成 short 使用基 base , which is 10 by default and must be between 2 and 36, or 0.

base is 0, the base is determined automatically using the following rules: If the byte array begins with "0x", it is assumed to be hexadecimal; if it begins with "0", it is assumed to be octal; otherwise it is assumed to be decimal.

返迴 0,若轉換失敗。

ok is not 0: if a conversion error occurs, * ok is set to false; otherwise * ok 被設為 true。

注意: The conversion of the number is performed in the default C locale, irrespective of the user's locale.

另請參閱 number ().

std::string QByteArray:: toStdString () const

返迴 std::string 對象帶有的數據包含在此 QByteArray .

此運算符主要用於傳遞 QByteArray 到接受 std::string 對象的函數。

該函數在 Qt 5.4 引入。

另請參閱 fromStdString () 和 QString::toStdString ().

uint QByteArray:: toUInt ( bool * ok = Q_NULLPTR, int base = 10) const

返迴字節數組被轉換成 無符號 int 使用基 base , which is 10 by default and must be between 2 and 36, or 0.

base is 0, the base is determined automatically using the following rules: If the byte array begins with "0x", it is assumed to be hexadecimal; if it begins with "0", it is assumed to be octal; otherwise it is assumed to be decimal.

返迴 0,若轉換失敗。

ok is not 0: if a conversion error occurs, * ok is set to false; otherwise * ok 被設為 true。

注意: The conversion of the number is performed in the default C locale, irrespective of the user's locale.

另請參閱 number ().

ulong QByteArray:: toULong ( bool * ok = Q_NULLPTR, int base = 10) const

返迴字節數組被轉換成 unsigned long int 使用基 base , which is 10 by default and must be between 2 and 36, or 0.

base is 0, the base is determined automatically using the following rules: If the byte array begins with "0x", it is assumed to be hexadecimal; if it begins with "0", it is assumed to be octal; otherwise it is assumed to be decimal.

返迴 0,若轉換失敗。

ok is not 0: if a conversion error occurs, * ok is set to false; otherwise * ok 被設為 true。

注意: The conversion of the number is performed in the default C locale, irrespective of the user's locale.

該函數在 Qt 4.1 引入。

另請參閱 number ().

qulonglong QByteArray:: toULongLong ( bool * ok = Q_NULLPTR, int base = 10) const

返迴字節數組被轉換成 unsigned long long 使用基 base , which is 10 by default and must be between 2 and 36, or 0.

base is 0, the base is determined automatically using the following rules: If the byte array begins with "0x", it is assumed to be hexadecimal; if it begins with "0", it is assumed to be octal; otherwise it is assumed to be decimal.

返迴 0,若轉換失敗。

ok is not 0: if a conversion error occurs, * ok is set to false; otherwise * ok 被設為 true。

注意: The conversion of the number is performed in the default C locale, irrespective of the user's locale.

另請參閱 number ().

ushort QByteArray:: toUShort ( bool * ok = Q_NULLPTR, int base = 10) const

返迴字節數組被轉換成 unsigned short 使用基 base , which is 10 by default and must be between 2 and 36, or 0.

base is 0, the base is determined automatically using the following rules: If the byte array begins with "0x", it is assumed to be hexadecimal; if it begins with "0", it is assumed to be octal; otherwise it is assumed to be decimal.

返迴 0,若轉換失敗。

ok is not 0: if a conversion error occurs, * ok is set to false; otherwise * ok 被設為 true。

注意: The conversion of the number is performed in the default C locale, irrespective of the user's locale.

另請參閱 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("Qt by THE QT COMPANY");
QByteArray y = x.toUpper();
// y == "QT BY THE QT COMPANY"
					

另請參閱 toLower () 和 8 位字符比較 .

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() 函數返迴 true in the C locale. This includes the ASCII characters '\t', '\n', '\v', '\f', '\r', and ' '.

範例:

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

不像 simplified (),trimmed() 隻留下內部空白。

另請參閱 simplified ().

void QByteArray:: truncate ( int pos )

截取字節數組按索引位置 pos .

pos 超越數組末尾,什麼都不發生。

範例:

QByteArray ba("Stockholm");
ba.truncate(5);             // ba == "Stock"
					

另請參閱 chop (), resize (),和 left ().

bool QByteArray:: operator!= (const QString & str ) const

返迴 true if this byte array is not equal to string str ;否則返迴 false .

The Unicode data is converted into 8-bit characters using QString::toUtf8 ().

比較是區分大小寫的。

可以禁用此運算符通過定義 QT_NO_CAST_FROM_ASCII 當編譯應用程序時。那麼需要調用 QString::fromUtf8 (), QString::fromLatin1 (),或 QString::fromLocal8Bit () explicitly if you want to convert the byte array to a QString before doing the comparison.

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

追加字節數組 ba 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 隱式共享 類。因此,若追加到空字節數組,那麼字節數組將僅僅共享的數據保持在 ba 。在這種情況下,不進行數據拷貝,花費 常量時間 。若共享實例被修改,它將被拷貝 (寫時拷貝),花費 綫性時間 .

若要追加到的字節數組非空,履行數據深拷貝,花費 綫性時間 .

This operation typically does not suffer from allocation overhead, because QByteArray preallocates extra space at the end of the data so that it may grow without reallocating for each append operation.

另請參閱 append () 和 prepend ().

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

這是重載函數。

追加字符串 str onto the end of this byte array and returns a reference to this byte array.

QByteArray &QByteArray:: operator+= ( char ch )

這是重載函數。

追加字符 ch onto the end of this byte array and returns a reference to this byte array.

QByteArray &QByteArray:: operator+= (const QString & str )

這是重載函數。

追加字符串 str onto the end of this byte array and returns a reference to this byte array. The Unicode data is converted into 8-bit characters using QString::toUtf8 ().

可以禁用此函數通過定義 QT_NO_CAST_TO_ASCII 當編譯應用程序時。那麼需要調用 QString::toUtf8 () (或 QString::toLatin1 () 或 QString::toLocal8Bit ()) explicitly if you want to convert the data to const char * .

bool QByteArray:: operator< (const QString & str ) const

返迴 true if this byte array is lexically less than string str ;否則返迴 false .

The Unicode data is converted into 8-bit characters using QString::toUtf8 ().

比較是區分大小寫的。

可以禁用此運算符通過定義 QT_NO_CAST_FROM_ASCII 當編譯應用程序時。那麼需要調用 QString::fromUtf8 (), QString::fromLatin1 (),或 QString::fromLocal8Bit () explicitly if you want to convert the byte array to a QString before doing the comparison.

bool QByteArray:: operator<= (const QString & str ) const

返迴 true if this byte array is lexically less than or equal to string str ;否則返迴 false .

The Unicode data is converted into 8-bit characters using QString::toUtf8 ().

比較是區分大小寫的。

可以禁用此運算符通過定義 QT_NO_CAST_FROM_ASCII 當編譯應用程序時。那麼需要調用 QString::fromUtf8 (), QString::fromLatin1 (),或 QString::fromLocal8Bit () explicitly if you want to convert the byte array to a QString before doing the comparison.

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

賦值 other 到此字節數組,並返迴此字節數組的引用。

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

這是重載函數。

賦值 str 到此字節數組。

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

移動賦值 other 到此 QByteArray 實例。

該函數在 Qt 5.2 引入。

bool QByteArray:: operator== (const QString & str ) const

返迴 true if this byte array is equal to string str ;否則返迴 false .

The Unicode data is converted into 8-bit characters using QString::toUtf8 ().

比較是區分大小寫的。

可以禁用此運算符通過定義 QT_NO_CAST_FROM_ASCII 當編譯應用程序時。那麼需要調用 QString::fromUtf8 (), QString::fromLatin1 (),或 QString::fromLocal8Bit () explicitly if you want to convert the byte array to a QString before doing the comparison.

bool QByteArray:: operator> (const QString & str ) const

返迴 true if this byte array is lexically greater than string str ;否則返迴 false .

The Unicode data is converted into 8-bit characters using QString::toUtf8 ().

比較是區分大小寫的。

可以禁用此運算符通過定義 QT_NO_CAST_FROM_ASCII 當編譯應用程序時。那麼需要調用 QString::fromUtf8 (), QString::fromLatin1 (),或 QString::fromLocal8Bit () explicitly if you want to convert the byte array to a QString before doing the comparison.

bool QByteArray:: operator>= (const QString & str ) const

返迴 true if this byte array is greater than or equal to string str ;否則返迴 false .

The Unicode data is converted into 8-bit characters using QString::toUtf8 ().

比較是區分大小寫的。

可以禁用此運算符通過定義 QT_NO_CAST_FROM_ASCII 當編譯應用程序時。那麼需要調用 QString::fromUtf8 (), QString::fromLatin1 (),或 QString::fromLocal8Bit () explicitly if you want to convert the byte array to a QString before doing the comparison.

QByteRef QByteArray:: operator[] ( int i )

返迴字節按索引位置 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 ba;
for (int i = 0; i < 10; ++i)
    ba[i] = 'A' + i;
// ba == "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.

另請參閱 at ().

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

這是重載函數。

char QByteArray:: operator[] ( int i ) const

這是重載函數。

如同 at( i ).

QByteRef QByteArray:: operator[] ( uint i )

這是重載函數。

相關非成員

quint16 qChecksum (const char * data , uint len )

Returns the CRC-16 checksum of the first len bytes of data .

The checksum is independent of the byte order (endianness) and will be calculated accorded to the algorithm published in ISO 3309 ( Qt::ChecksumIso3309 ).

注意: This function is a 16-bit cache conserving (16 entry table) implementation of the CRC-16-CCITT algorithm.

quint16 qChecksum (const char * data , uint len , Qt::ChecksumType standard )

Returns the CRC-16 checksum of the first len bytes of data .

The checksum is independent of the byte order (endianness) and will be calculated accorded to the algorithm published in standard .

注意: This function is a 16-bit cache conserving (16 entry table) implementation of the CRC-16-CCITT algorithm.

該函數在 Qt 5.9 引入。

QByteArray qCompress (const uchar * data , int nbytes , int compressionLevel = -1)

這是重載函數。

Compresses the first nbytes of data at compression level compressionLevel and returns the compressed data in a new byte array.

QByteArray qCompress (const QByteArray & data , int compressionLevel = -1)

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 (i.e. smaller compressed data) at the cost of using a slower algorithm. Smaller values (8, 7, ..., 1) 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.

另請參閱 qUncompress ().

QByteArray qUncompress (const QByteArray & data )

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

返迴空 QByteArray 若輸入數據被破壞。

This function will uncompress data compressed with qCompress () from this and any earlier Qt version, back to Qt 3.1 when this feature was added.

注意: If you want to use this function 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.

另請參閱 qCompress ().

int qsnprintf ( char * str , size_t n , const char * fmt , ... )

可移植 snprintf() 函數,調用 qvsnprintf。

fmt printf() format string. The result is put into str , which is a buffer of at least n 字節。

警告: Call this function only when you know what you are doing since it shows different behavior on certain platforms. Use QString::asprintf () to format a string instead.

另請參閱 qvsnprintf () 和 QString::asprintf ().

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

安全 strcmp() 函數。

比較 str1 and str2 。返迴負值若 str1 小於 str2 , 0 if str1 等於 str2 或正值若 str1 大於 str2 .

特殊情況 1:返迴 0 若 str1 and str2 兩者是 0。

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

另請參閱 qstrncmp (), qstricmp (), qstrnicmp (),和 8 位字符比較 .

char * qstrcpy ( char * dst , const char * src )

Copies all the characters up to and including the '\0' from src into dst and returns a pointer to dst 。若 src is 0, it immediately returns 0.

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

另請參閱 qstrncpy ().

char * qstrdup (const char * src )

返迴復製字符串。

Allocates space for a copy of src , copies it, and returns a pointer to the copy. If src is 0, it immediately returns 0.

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

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

安全 stricmp() 函數。

比較 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 小於 str2 , 0 if str1 等於 str2 或正值若 str1 大於 str2 .

特殊情況 1:返迴 0 若 str1 and str2 兩者是 0。

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

另請參閱 qstrcmp (), qstrncmp (), qstrnicmp (),和 8 位字符比較 .

uint qstrlen (const char * str )

安全 strlen() 函數。

Returns the number of characters that precede the terminating '\0', or 0 if str 為 0。

另請參閱 qstrnlen ().

int qstrncmp (const char * str1 , const char * str2 , uint len )

安全 strncmp() 函數。

比較最多 len bytes of str1 and str2 .

Returns a negative value if str1 小於 str2 , 0 if str1 等於 str2 或正值若 str1 大於 str2 .

特殊情況 1:返迴 0 若 str1 and str2 兩者是 0。

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

另請參閱 qstrcmp (), qstricmp (), qstrnicmp (),和 8 位字符比較 .

char * qstrncpy ( char * dst , const char * src , uint len )

安全 strncpy() 函數。

拷貝最多 len 字節來自 src (stopping at len or the terminating '\0' whichever comes first) into dst and returns a pointer to dst . Guarantees that dst is '\0'-terminated. If src or dst is 0, returns 0 immediately.

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

注意: When compiling with Visual C++ compiler version 14.00 (Visual C++ 2005) or later, internally the function strncpy_s will be used.

另請參閱 qstrcpy ().

int qstrnicmp (const char * str1 , const char * str2 , uint len )

安全 strnicmp() 函數。

比較最多 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 小於 str2 , 0 if str1 等於 str2 或正值若 str1 大於 str2 .

特殊情況 1:返迴 0 若 str1 and str2 兩者是 0。

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

另請參閱 qstrcmp (), qstrncmp (), qstricmp (),和 8 位字符比較 .

uint qstrnlen (const char * str , uint maxlen )

安全 strnlen() 函數。

Returns the number of characters that precede the terminating '\0', but at most maxlen 。若 str is 0, returns 0.

該函數在 Qt 4.2 引入。

另請參閱 qstrlen ().

int qvsnprintf ( char * str , size_t n , const char * fmt , va_list ap )

可移植 vsnprintf() 函數。會調用 ::vsnprintf() , ::_vsnprintf() ,或 ::vsnprintf_s depending on the system, or fall back to an internal version.

fmt printf() format string. The result is put into str , which is a buffer of at least n 字節。

The caller is responsible to call va_end() on ap .

警告: Since vsnprintf() shows different behavior on certain platforms, you should not rely on the return value or on the fact that you will always get a 0 terminated string back.

Ideally, you should never call this function but use QString::asprintf () 代替。

另請參閱 qsnprintf () 和 QString::asprintf ().

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

這是重載函數。

返迴 true 若字節數組 a1 不等於字節數組 a2 ;否則返迴 false .

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

這是重載函數。

返迴 true 若字節數組 a1 不等於字符串 a2 ;否則返迴 false .

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

這是重載函數。

返迴 true 若字符串 a1 不等於字節數組 a2 ;否則返迴 false .

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

返迴的字節數組結果是串聯字節數組 a1 和字節數組 a2 .

另請參閱 QByteArray::operator+= ().

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

這是重載函數。

返迴的字節數組結果是串聯字節數組 a1 and string a2 .

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

這是重載函數。

返迴的字節數組結果是串聯字節數組 a1 和字符 a2 .

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

這是重載函數。

返迴的字節數組結果是串聯字符串 a1 和字節數組 a2 .

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

這是重載函數。

返迴的字節數組結果是串聯字符 a1 和字節數組 a2 .

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

這是重載函數。

返迴 true 若字節數組 a1 詞匯上小於字節數組 a2 ;否則返迴 false .

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

這是重載函數。

返迴 true 若字節數組 a1 詞法上小於字符串 a2 ;否則返迴 false .

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

這是重載函數。

返迴 true 若字符串 a1 詞匯上小於字節數組 a2 ;否則返迴 false .

QDataStream & operator<< ( QDataStream & out , const QByteArray & ba )

寫入字節數組 ba 到流 out 並返迴流引用。

另請參閱 序列化 Qt 數據類型 .

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

這是重載函數。

返迴 true 若字節數組 a1 詞法上 <= 字節數組 a2 ;否則返迴 false .

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

這是重載函數。

返迴 true 若字節數組 a1 詞法上 <= 字符串 a2 ;否則返迴 false .

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

這是重載函數。

返迴 true 若字符串 a1 詞法上 <= 字節數組 a2 ;否則返迴 false .

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

這是重載函數。

返迴 true 若字節數組 a1 等於字節數組 a2 ;否則返迴 false .

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

這是重載函數。

返迴 true 若字節數組 a1 等於字符串 a2 ;否則返迴 false .

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

這是重載函數。

返迴 true 若字符串 a1 等於字節數組 a2 ;否則返迴 false .

bool operator> (const QByteArray & a1 , const QByteArray & a2 )

這是重載函數。

返迴 true 若字節數組 a1 詞法上大於字節數組 a2 ;否則返迴 false .

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

這是重載函數。

返迴 true 若字節數組 a1 詞法上大於字符串 a2 ;否則返迴 false .

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

這是重載函數。

返迴 true 若字符串 a1 詞法上大於字節數組 a2 ;否則返迴 false .

bool operator>= (const QByteArray & a1 , const QByteArray & a2 )

這是重載函數。

返迴 true 若字節數組 a1 詞法上大於等於字節數組 a2 ;否則返迴 false .

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

這是重載函數。

返迴 true 若字節數組 a1 詞法上大於等於字符串 a2 ;否則返迴 false .

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

這是重載函數。

返迴 true 若字符串 a1 詞法上大於等於字節數組 a2 ;否則返迴 false .

QDataStream & operator>> ( QDataStream & in , QByteArray & ba )

讀取字節數組到 ba 從流 in 並返迴流引用。

另請參閱 序列化 Qt 數據類型 .

宏文檔編製

QByteArrayLiteral ( ba )

宏生成數據為 QByteArray 從字符串文字 ba 在編譯時。創建 QByteArray 是自由的在此情況下,且生成的字節數組數據是存儲在編譯對象文件的隻讀段中。

例如:

QByteArray ba = QByteArrayLiteral("byte array contents");
					

使用 QByteArrayLiteral 而不是帶雙引號的純 C++ 字符串文字可以顯著加速創建 QByteArray 實例從編譯時的已知數據。

另請參閱 QStringLiteral .

QT_NO_CAST_FROM_BYTEARRAY

禁用自動轉換從 QByteArray 到 const char * 或 const void *。

另請參閱 QT_NO_CAST_TO_ASCII and QT_NO_CAST_FROM_ASCII .