QByteArray Class Reference

[ QtCore module]

QByteArray 类提供字节数组。 更多...

方法

Static Methods

Special Methods


详细描述

This class can be pickled.

A Python string object may be used whenever a QByteArray is expected.

QByteArray 类提供字节数组。

QByteArray can be used to store both raw bytes (including '\0's) and traditional 8-bit '\0'-terminated strings. Using QByteArray is much more convenient than using const char * . Behind the scenes, it always ensures that the data is followed by a '\0' terminator, and uses implicit sharing (copy-on-write) to reduce memory usage and avoid needless copying of data.

除 QByteArray 外,Qt 还提供 QString class to store string data. For most purposes, QString is the class you want to use. It stores 16-bit Unicode characters, making it easy to store non-ASCII/non-Latin-1 characters in your application. Furthermore, QString is used throughout in the Qt API. The two main cases where QByteArray is appropriate are when you need to store raw binary data, and when memory conservation is critical (e.g., with Qt for Embedded Linux).

One way to initialize a QByteArray is simply to pass a const char * to its constructor. For example, the following code creates a byte array of size 5 containing the data "Hello":

 QByteArray ba("Hello");
			

Although the 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 deep copy to occur.

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

A QByteArray can embed '\0' bytes. The size () function always returns the size of the whole array, including embedded '\0' bytes. If you want to obtain the length of the data up to and excluding the first '\0' character, call qstrlen () 在 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 () 或 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"
			

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

If you want to find all occurrences of a particular character or substring in a QByteArray, use 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.

QByteArrays 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
			

All functions except 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 位字符比较

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 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 QStrings since they represent characters using Unicode.


方法文档编制

QByteArray.__init__ ( self )

Constructs an empty byte array.

另请参阅 isEmpty ().

QByteArray.__init__ ( self , int  size , str  c )

Constructs a byte array initialized with the string str .

QByteArray makes a deep copy of the string data.

QByteArray.__init__ ( self , QByteArray   a )

Constructs a byte array containing the first size bytes of array data .

data is 0, a null byte array is constructed.

QByteArray makes a deep copy of the string data.

另请参阅 fromRawData ().

QByteArray QByteArray.append ( self , QByteArray   a )

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

范例:

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

This is the same as insert( size (), ba ).

注意: QByteArray 隐式共享 class. Consequently, if this is an empty QByteArray ,那么 this will just share the data held in ba . In this case, no copying of data is done, taking constant time . If a shared instance is modified, it will be copied (copy-on-write), taking linear time .

this is not an empty QByteArray , a deep copy of the data is performed, taking linear time .

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 ( self , QString  s )

这是重载函数。

Appends the string str to this byte array. The Unicode data is converted into 8-bit characters using QString.toAscii ().

QString contains non-ASCII Unicode characters, using this function can lead to loss of information. You can disable this function by defining QT_NO_CAST_TO_ASCII when you compile your applications. You then need to call QString.toAscii () (or QString.toLatin1 () 或 QString.toUtf8 () 或 QString.toLocal8Bit ()) explicitly if you want to convert the data to const char * .

str QByteArray.at ( self , int  i )

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

i must be a valid index position in the byte array (i.e., 0 <= i < size ()).

另请参阅 operator[] ().

int QByteArray.capacity ( self )

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 () and squeeze ().

QByteArray.chop ( self , 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 ().

QByteArray.clear ( self )

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

另请参阅 resize () and isEmpty ().

bool QByteArray.contains ( self , QByteArray   a )

Returns true if the byte array contains an occurrence of the byte array ba ;否则返回 false。

另请参阅 indexOf () and count ().

int QByteArray.count ( self , QByteArray   a )

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

另请参阅 contains () 和 indexOf ().

int QByteArray.count ( self )

这是重载函数。

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

str QByteArray.data ( self )

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 () is faster because it never causes a deep copy to occur.

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

The following example makes a copy of the char* returned by data(), but it will corrupt the heap and cause a crash because it does not allocate a byte for the '\0' at the end:

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

This one allocates the correct amount of space:

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

Note: A QByteArray can store any byte values including '\0's, but most functions that take char * arguments assume that the data ends at the first '\0' they encounter.

另请参阅 constData () 和 operator[] ().

bool QByteArray.endsWith ( self , QByteArray   a )

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

范例:

 QByteArray url("http://qt.nokia.com/index.html");
 if (url.endsWith(".html"))
     ...
			

另请参阅 startsWith () 和 right ().

QByteArray QByteArray.fill ( self , str  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 beforehand.

范例:

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

另请参阅 resize ().

QByteArray QByteArray.fromBase64 ( 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 2045 .

另请参阅 toBase64 ().

QByteArray QByteArray.fromHex ( 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 ().

QByteArray QByteArray.fromPercentEncoding ( QByteArray   input , str  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!"
			

该函数在 Qt 4.4 引入。

另请参阅 toPercentEncoding () 和 QUrl.fromPercentEncoding ().

QByteArray QByteArray.fromRawData (str)

构造 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 does not take ownership of 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 ().

int QByteArray.indexOf ( self , QByteArray   ba , int  from  = 0)

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 ( self , QString  str , int  from  = 0)

这是重载函数。

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

QString contains non-ASCII Unicode characters, using this function can lead to loss of information. You can disable this function by defining QT_NO_CAST_TO_ASCII when you compile your applications. You then need to call QString.toAscii () (or QString.toLatin1 () 或 QString.toUtf8 () 或 QString.toLocal8Bit ()) explicitly if you want to convert the data to const char * .

QByteArray QByteArray.insert ( self , int  i , QByteArray   a )

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 ( self , int  i , QString  s )

这是重载函数。

Inserts the string str at index position i 在 byte array. The Unicode data is converted into 8-bit characters 使用 QString.toAscii ().

i 大于 size (), the array is first extended 使用 resize ().

QString contains non-ASCII Unicode characters, using this function can lead to loss of information. You can disable this function by defining QT_NO_CAST_TO_ASCII when you compile your applications. You then need to call QString.toAscii () (or QString.toLatin1 () 或 QString.toUtf8 () 或 QString.toLocal8Bit ()) explicitly if you want to convert the data to const char * .

bool QByteArray.isEmpty ( self )

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
			

另请参阅 size ().

bool QByteArray.isNull ( self )

Returns 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 ( self , QByteArray   ba , int  from  = -1)

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 ( self , QString  str , int  from  = -1)

这是重载函数。

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

QString contains non-ASCII Unicode characters, using this function can lead to loss of information. You can disable this function by defining QT_NO_CAST_TO_ASCII when you compile your applications. You then need to call QString.toAscii () (or QString.toLatin1 () 或 QString.toUtf8 () 或 QString.toLocal8Bit ()) explicitly if you want to convert the data to const char * .

QByteArray QByteArray.left ( self , int  len )

返回的字节数组包含最左 len bytes of this byte array.

返回整个字节数组若 len 大于 size ().

范例:

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

另请参阅 right (), mid (), startsWith (),和 truncate ().

QByteArray QByteArray.leftJustified ( self , int  width , str  fill  = ' ', bool  truncate  = False)

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 ( self )

如同 size ().

QByteArray QByteArray.mid ( self , int  pos , int  length  = -1)

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

len is -1 (the default), or 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 ().

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 () and toInt ().

QByteArray QByteArray.number (float  n , str  format  = 'g', int  precision  = 6)

这是重载函数。

另请参阅 toUInt ().

QByteArray QByteArray.number (int  n , int  base  = 10)

这是重载函数。

另请参阅 toLongLong ().

QByteArray QByteArray.number (int  n , int  base  = 10)

这是重载函数。

另请参阅 toULongLong ().

QByteArray QByteArray.prepend ( self , QByteArray   a )

前置字节数组 ba to this byte array and returns a reference to this byte array.

范例:

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

这如同 insert(0, ba ).

注意: QByteArray 隐式共享 class. Consequently, if this is an empty QByteArray ,那么 this will just share the data held in ba . In this case, no copying of data is done, taking constant time . If a shared instance is modified, it will be copied (copy-on-write), taking linear time .

this is not an empty QByteArray , a deep copy of the data is performed, taking linear time .

另请参阅 append () and insert ().

QByteArray.push_back ( self , QByteArray   a )

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

QByteArray.push_front ( self , QByteArray   a )

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

QByteArray QByteArray.remove ( self , int  index , 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 () and replace ().

QByteArray QByteArray.repeated ( self , int  times )

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 ( self , int  index , int  len , QByteArray   s )

替换 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 () and remove ().

QByteArray QByteArray.replace ( self , QByteArray   before , QByteArray   after )

这是重载函数。

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

注意:这可以改变字节数组的长度。

QByteArray QByteArray.replace ( self , QString  before , QByteArray   after )

这是重载函数。

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

该函数在 Qt 4.7 引入。

QByteArray.reserve ( self , int  size )

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 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 () and capacity ().

QByteArray.resize ( self , 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 ().

QByteArray QByteArray.right ( self , int  len )

返回的字节数组包含最右 len 字节在此字节数组。

返回整个字节数组若 len 大于 size ().

范例:

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

另请参阅 endsWith (), left (),和 mid ().

QByteArray QByteArray.rightJustified ( self , int  width , str  fill  = ' ', bool  truncate  = False)

Returns a byte array of size width that contains the 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 在位置 width .

范例:

 QByteArray x("apple");
 QByteArray y = x.rightJustified(8, '.');    // y == "...apple"
			

另请参阅 leftJustified ().

QByteArray QByteArray.setNum ( self , 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.

范例:

 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 () and toInt ().

QByteArray QByteArray.setNum ( self , float  n , str  format  = 'g', int  precision  = 6)

这是重载函数。

另请参阅 toUInt ().

QByteArray QByteArray.setNum ( self , int  n , int  base  = 10)

这是重载函数。

另请参阅 toShort ().

QByteArray QByteArray.setNum ( self , int  n , int  base  = 10)

这是重载函数。

另请参阅 toUShort ().

QByteArray QByteArray.simplified ( self )

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() function returns true. 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 ( self )

返回此字节数组的字节数。

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 deep copy is created.

范例:

 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 () and resize ().

list-of-QByteArray QByteArray.split ( self , str  sep )

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.

QByteArray.squeeze ( self )

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 () and capacity ().

bool QByteArray.startsWith ( self , QByteArray   a )

Returns true if this byte array starts with byte array ba ;否则返回 false。

范例:

 QByteArray url("ftp://ftp.qt.nokia.com/");
 if (url.startsWith("ftp:"))
     ...
			

另请参阅 endsWith () 和 left ().

QByteArray.swap ( self , QByteArray   other )

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

该函数在 Qt 4.8 引入。

QByteArray QByteArray.toBase64 ( self )

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

另请参阅 fromBase64 ().

(float, bool  ok ) QByteArray.toDouble ( self )

Returns the byte array converted to a double 值。

Returns 0.0 if the conversion fails.

ok is not 0: if a conversion error occurs, * ok is set to false; otherwise * ok is set to 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, bool  ok ) QByteArray.toFloat ( self )

Returns the byte array converted to a float 值。

Returns 0.0 if the conversion fails.

ok is not 0: if a conversion error occurs, * ok is set to false; otherwise * ok is set to true.

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

另请参阅 number ().

QByteArray QByteArray.toHex ( self )

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

另请参阅 fromHex ().

(int, bool  ok ) QByteArray.toInt ( self , int  base  = 10)

Returns the byte array converted to an 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 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
			

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

另请参阅 number ().

(int, bool  ok ) QByteArray.toLong ( self , int  base  = 10)

Returns the byte array converted to a 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 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
			

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

该函数在 Qt 4.1 引入。

另请参阅 number ().

(int, bool  ok ) QByteArray.toLongLong ( self , int  base  = 10)

Returns the byte array converted to a long long 使用 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 is set to true.

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

另请参阅 number ().

QByteArray QByteArray.toLower ( self )

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

范例:

 QByteArray x("Qt by NOKIA");
 QByteArray y = x.toLower();
 // y == "qt by nokia"
			

另请参阅 toUpper () and 8 位 Character Comparisons .

QByteArray QByteArray.toPercentEncoding ( self , QByteArray   exclude  = QByteArray(), QByteArray   include  = QByteArray(), str  percent  = '%')

Returns a URI/URL-style percent-encoded copy of this byte array. 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 include 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 ().

(int, bool  ok ) QByteArray.toShort ( self , int  base  = 10)

Returns the byte array converted to a short 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 is set to true.

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

另请参阅 number ().

(int, bool  ok ) QByteArray.toUInt ( self , int  base  = 10)

Returns the byte array converted to an 无符号 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 is set to true.

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

另请参阅 number ().

(int, bool  ok ) QByteArray.toULong ( self , int  base  = 10)

Returns the byte array converted to an unsigned 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 is set to true.

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

该函数在 Qt 4.1 引入。

另请参阅 number ().

(int, bool  ok ) QByteArray.toULongLong ( self , int  base  = 10)

Returns the byte array converted to an unsigned long long 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 is set to true.

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

另请参阅 number ().

QByteArray QByteArray.toUpper ( self )

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

范例:

 QByteArray x("Qt by NOKIA");
 QByteArray y = x.toUpper();
 // y == "QT BY NOKIA"
			

另请参阅 toLower () and 8 位 Character Comparisons .

(int, bool  ok ) QByteArray.toUShort ( self , int  base  = 10)

Returns the byte array converted to an unsigned short 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 is set to true.

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

另请参阅 number ().

QByteArray QByteArray.trimmed ( self )

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

Whitespace means any character for which the standard C++ isspace() function returns true. 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() leaves internal whitespace alone.

另请参阅 simplified ().

QByteArray.truncate ( self , int  pos )

Truncates the byte array at index position pos .

pos is beyond the end of the array, nothing happens.

范例:

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

另请参阅 chop (), resize (),和 left ().

QByteArray QByteArray.__add__ ( self , QByteArray   a2 )

QString QByteArray.__add__ ( self , QString  s )

int QByteArray.__contains__ ( self , QByteArray   a )

bool QByteArray.__eq__ ( self , QString  s2 )

bool QByteArray.__eq__ ( self , QByteArray   a2 )

bool QByteArray.__ge__ ( self , QString  s2 )

bool QByteArray.__ge__ ( self , QByteArray   a2 )

str QByteArray.__getitem__ ( self , int  i )

QByteArray QByteArray.__getitem__ ( self , slice  slice )

bool QByteArray.__gt__ ( self , QString  s2 )

bool QByteArray.__gt__ ( self , QByteArray   a2 )

int QByteArray.__hash__ ( self )

QByteArray QByteArray.__iadd__ ( self , QByteArray   a )

QByteArray QByteArray.__iadd__ ( self , QString  s )

QByteArray QByteArray.__imul__ ( self , int  m )

bool QByteArray.__le__ ( self , QString  s2 )

bool QByteArray.__le__ ( self , QByteArray   a2 )

QByteArray.__len__ ( self )

bool QByteArray.__lt__ ( self , QString  s2 )

bool QByteArray.__lt__ ( self , QByteArray   a2 )

QByteArray QByteArray.__mul__ ( self , int  m )

bool QByteArray.__ne__ ( self , QString  s2 )

bool QByteArray.__ne__ ( self , QByteArray   a2 )

str QByteArray.__repr__ ( self )

str QByteArray.__str__ ( self )