Qt--qbytearray and Qbuffer

Source: Internet
Author: User
Tags constructor

1) The Qbytearray class provides an array of [bytes]. The Qbytearray can store both the original bytes (including ' the ' + ') and can be used to store a 8-bit string that ends with '/' on the Convention. < can be interpreted as an array of characters, charstr[] = {' h ', ' e ', ' l ', ' l ', ' o ', ' s '} or char str = ' Hello ' >. Because the Qbytearray package has a lot of functionality and is much more convenient to use than char*, in its internal implementation, it ensures that all data ends with '% ' and uses implicit data sharing (Copy-on-write) to reduce memory consumption and unnecessary data copying.


2) In addition to QBYTEARRAY,QT, the Qstring class is provided to store strings, and in most cases we are using qstring. Qstring stores 16-bit Unicode code, which is easy to store non-ASCII or Lantin1 encodings, and qstring is common in all qtapi.


3) There are two situations that are more appropriate to use Qbytearray, the first is to store pure binary data (raw binary) or 8-bit encoded text strings, the second case is in memory resources is very valuable, such as Qt for Embedded Linux.

----------------------------------------------------------------------

4) An initialization Qbytearray method is to pass in a const char* to its constructor function. At this point, the Qbytearray performs a deep copy (in depth copy, such as a value type), and if you do not want to perform a deep copy for efficiency reasons, use Qbytearray::fromrawdata (const char * data, int siz)/* returned  The Qbytearray object will be associated with the data pointer */.

Qbytearrayarray ("Hello"), the size of the array () is 5, but the actual footprint is 6 because it has to store an extra '% ' at the end.


============================= Common operation function ==============================
1, int qbytearray::size () const/* sizeof () */
If Qbytearray does not contain trailing terminators when it is created from raw data, Qbytearray does not automatically invoke additions unless created by deep copy:
Qbytearray BA ("Hello");
int n = ba.size (); n = = 5
Ba.data () [0]; Returns ' H '
Ba.data () [4]; Returns ' O '
Ba.data () [5]; Returns ' + '

----------------------------------------------------------------------
2, like the ordinary C + + array, you can also use [] to visit its specific table corresponding to the byte, for non-const Qbytearray, can be directly assigned:
1. Qbytearray array;
2. Array.resize (5);
3. Array [0] = 0x3c;
4. Array [1] = 0xb8;
5. Array [2] = 0x64;
6. Array [3] = 0x18;
7. Array [4] = 0XCA;

----------------------------------------------------------------------
3. For read-only operations, use "at ()" because it avoids deep copies and is faster and more efficient than using "[]":
1. for (int i = 0; I < array.size (); + + i) {
2. if (array.at (i) >= ' a ' && array.at (i) <= ' F ')
3. cout << "Found character in range [a–f]" << Endl;
4.}

----------------------------------------------------------------------


4. Use Left (), right (), or mid to remove more than one character at a time.

1) Qbytearray Qbytearray:: Left (int len) const
The entire byte array is returned if Len is greater than size ().
Example:
Qbytearray x ("Pineapple");
Qbytearray y = x.left (4);
y = = "Pine"


2) qbytearray qbytearray::right (int len) const
The entire byte array is returned if Len is greater than size ().
Example:
Qbytearray x ("Pineapple");
Qbytearray y = x.right (5);
y = = "Apple"


3) Qbytearray qbytearray::mid (int pos, int len =-1) const

With POS as the starting point, returns an array of the specified byte length, if Len is-1 (the default), or Pos+len >= size (), returns all bytes from the specified POS until the end of the byte array.
Example:
Qbytearray x ("Five pineapples");
Qbytearray y = X.mid (5, 4); y = = "Pine"
Qbytearray z = x.mid (5); z = = "Pineapples"
----------------------------------------------------------------------

5. Char * Qbytearray::d ata ()//returns A pointer to the data stored in the byte array

const char * qbytearray::constdata () const//returns A pointer to the data stored in the byte array

Data () or constdata () can be used to obtain a pointer to the Qbytearray's real data, which is valid until the Non-const function of the Qbytearray is called.

6, UINT Qstrlen (const char * str)/* as strlen () in C + + */

Returns the number of characters that precede (precedes) the terminating ', or 0 if STR is 0.
Return values for >>>size () and Qtrlen ():
Qbytearray array = "Hello world!";
printf ("%d\n", Array.size ()-1);
printf ("%c\n", Array.data () [Array.size ()]);
printf ("%d\n", Qstrlen ((const char*) array.data ()));


7. Qbytearray provides many ways to modify bytes: Append (), prepend (), insert (), replace () and remove ().

As shown below:
Qbytearray x ("and");
X.prepend ("Rock"); x = = "Rock and"
X.append ("roll"); x = = "Rock and Roll"
X.replace (5, 3, "&"); x = = "Rock & Roll"

=====================================================================

8. The Qbuffer class is an interface for an input/output device that operates Qbytearray. Theqbuffer class provides aqiodeviceinterface for a qbytearray.

Constructor: Qbuffer (Qbytearray * ByteArray, qobject * parent = 0)

----------------------------------------------------------------------

The Qbuffer class is used to read and write memory caches. Use open () to turn on the cache and set the mode (read only, write-only, and so on) before use.

Qdatastream and Qtextstream can also be constructed with a Qbytearray parameter that creates and opens an internal qbuffer.

Qdatastream::qdatastream (Qbytearray * A, Qiodevice::openmode mode)

/*the Qdatastream class provides serialization of binary data to a qiodevice*/

----------------------------------------------------------------------

Qtextstream (Qbytearray * array, Qiodevice::openmode OpenMode = qiodevice::readwrite)

----------------------------------------------------------------------

Const Qbytearray & Qbuffer::d ata () const//returns The data contained in the buffer.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.