Qt Toolkit - QByteArray Class

| Classes - Annotated - Tree - Functions - Home - Structure

# QByteArray Class Reference

The QByteArray class provides an array of bytes. More...

`#include <qcstring.h>`

Inherits QArray.

Inherited by QBitArray and QCString.

List of all member functions.

## Public Members

- QByteArray ()
- QByteArray ( int size )
- QByteArray ( const QByteArray & a )
- ~QByteArray ()
- QByteArray& operator= ( const QByteArray & a )
- char* data () const
- uint nrefs () const
- uint size () const
- uint count () const
- bool isEmpty () const
- bool isNull () const
- bool resize ( uint size )
- bool truncate ( uint pos )
- bool fill ( const char & d, int size = -1 )
- virtual void detach ()
- QByteArray copy () const
- QByteArray& assign ( const QByteArray & a )
- QByteArray& assign ( const char * a, uint n )
- QByteArray& duplicate ( const QByteArray & a )
- QByteArray& duplicate ( const char * a, uint n )
- QByteArray& setRawData ( const char * a, uint n )
- void resetRawData ( const char * a, uint n )
- int find ( const char & d, uint i=0 ) const
- int contains ( const char & d ) const
- void sort ()
- int bsearch ( const char & d ) const
- char& operator[] ( int i ) const
- char& at ( uint i ) const
- operator const char* ()const
- bool operator== ( const QByteArray & a ) const
- bool operator!= ( const QByteArray & a ) const
- Iterator begin ()
- Iterator end ()
- ConstIterator begin () const
- ConstIterator end () const

## Protected Members

- QByteArray ( int, int )

## Related Functions

(Note that these are not member functions.)

- Q_UINT16 qChecksum (const char * data, uint len)
- QDataStream & operator<< (QDataStream & s, const QByteArray & a)
- QDataStream & operator>> (QDataStream & s, QByteArray & a)

## Detailed Description

The QByteArray class provides an array of bytes.

The QByteArray class provides an explicitly shared array of bytes. It is useful for manipulating memory areas with custom data. QByteArray is implemented as QArray. See the QArray documentation for further information.

## Member Function Documentation

### QByteArray::QByteArray ()

Constructs a null array.

See also isNull().

### QByteArray::QByteArray ( const QByteArray & a )

Constructs a shallow copy of a.

See also assign().

### QByteArray::QByteArray ( int size )

Constructs an array with room for size elements. Makes a null array if size == 0.

Note that the elements are not initialized.

See also resize() and isNull().

### QByteArray::QByteArray ( int, int ) `[protected]`

Constructs an array without allocating array space. The arguments should be (0, 0). Use at own risk.

### QByteArray::~QByteArray ()

Dereferences the array data and deletes it if this was the last reference.

### QByteArray::operator const char * () const

Cast operator. Returns a pointer to the array.

See also data().

### QByteArray & QByteArray::assign ( const QByteArray & a )

Shallow copy. Dereferences the current array and references the data contained in a instead. Returns a reference to this array.

See also operator=().

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

Shallow copy. Dereferences the current array and references the array data data, which contains size elements. Returns a reference to this array.

Do not delete data later, QArray takes care of that.

### char & QByteArray::at ( uint index ) const

Returns a reference to the element at position index in the array.

This can be used to both read and set an element.

See also operator[]().

### ConstIterator QByteArray::begin () const

Returns a const iterator pointing at the beginning of this array. This iterator can be used as the iterators of QValueList and QMap for example. In fact it does not only behave like a usual pointer: It is a pointer.

### Iterator QByteArray::begin ()

Returns an iterator pointing at the beginning of this array. This iterator can be used as the iterators of QValueList and QMap for example. In fact it does not only behave like a usual pointer: It is a pointer.

### int QByteArray::bsearch ( const char & v ) const

In a sorted array, finds the first occurrence of v using binary search. For a sorted array, this is generally much faster than find(), which does a linear search.

Returns the position of v, or -1 if v could not be found.

See also sort() and find().

### int QByteArray::contains ( const char & v ) const

Returns the number of times v occurs in the array.

See also find().

### QByteArray QByteArray::copy () const

Returns a deep copy of this array.

See also detach() and duplicate().

### uint QByteArray::count () const

Returns the same as size().

See also size().

### char * QByteArray::data () const

Returns a pointer to the actual array data.

The array is a null array if data() == 0 (null pointer).

See also isNull().

### void QByteArray::detach () `[virtual]`

Detaches this array from shared array data, i.e. makes a private, deep copy of the data.

Copying will only be performed if the reference count is greater than one.

See also copy().

Reimplemented from QGArray.

### QByteArray & QByteArray::duplicate ( const QByteArray & a )

Deep copy. Dereferences the current array and obtains a copy of the data contained in a instead. Returns a reference to this array.

See also copy().

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

Deep copy. Dereferences the current array and obtains a copy of the array data data instead. Returns a reference to this array.

See also copy().

### ConstIterator QByteArray::end () const

Returns a const iterator pointing behind the last element of this array. This iterator can be used as the iterators of QValueList and QMap for example. In fact it does not only behave like a usual pointer: It is a pointer.

### Iterator QByteArray::end ()

Returns an iterator pointing behind the last element of this array. This iterator can be used as the iterators of QValueList and QMap for example. In fact it does not only behave like a usual pointer: It is a pointer.

### bool QByteArray::fill ( const char & v, int size = -1 )

Fills the array with the value v. If size is specified as different from -1, then the array will be resized before filled.

Returns TRUE if successful, or FALSE if the memory cannot be allocated (only when size != -1).

See also resize().

### int QByteArray::find ( const char & v, uint index=0 ) const

Finds the first occurrence of v, starting at position index.

Returns the position of v, or -1 if v could not be found.

See also contains().

### bool QByteArray::isEmpty () const

Returns TRUE if the array is empty, i.e. size() == 0, otherwise FALSE.

isEmpty() is equivalent with isNull() for QArray. Note that this is not the case for QString::isEmpty().

### bool QByteArray::isNull () const

Returns TRUE if the array is null, otherwise FALSE.

A null array has size() == 0 and data() == 0.

### uint QByteArray::nrefs () const

Returns the reference count for the shared array data. This reference count is always greater than zero.

### bool QByteArray::operator!= ( const QByteArray & a ) const

Returns TRUE if this array is different from a, otherwise FALSE.

The two arrays are bitwise compared.

See also operator==().

### QByteArray & QByteArray::operator= ( const QByteArray & a )

Assigns a shallow copy of a to this array and returns a reference to this array.

Equivalent to assign( a ).

### bool QByteArray::operator== ( const QByteArray & a ) const

Returns TRUE if this array is equal to a, otherwise FALSE.

The two arrays are bitwise compared.

See also operator!=().

### char & QByteArray::operator[] ( int index ) const

Returns a reference to the element at position index in the array.

This can be used to both read and set an element. Equivalent to at().

See also at().

### void QByteArray::resetRawData ( const char * data, uint size )

Resets raw data that was set using setRawData().

The arguments must be the data and length that were passed to setRawData(). This is for consistency checking.

See also setRawData().

### bool QByteArray::resize ( uint size )

Resizes (expands or shrinks) the array to size elements. The array becomes a null array if size == 0.

Returns TRUE if successful, or FALSE if the memory cannot be allocated.

New elements will not be initialized.

See also size().

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

Sets raw data and returns a reference to the array.

Dereferences the current array and sets the new array data to data and the new array size to size. Do not attempt to resize or re-assign the array data when raw data has been set. Call resetRawData(d,len) to reset the array.

Setting raw data is useful because it sets QArray data without allocating memory or copying data.

Example I (intended use):

        static char bindata[] = { 231, 1, 44, ... };
        QByteArray  a;
        a.setRawData( bindata, sizeof(bindata) );   // a points to bindata
        QDataStream s( a, IO_ReadOnly );            // open on a's data
        s >> <something>;                           // read raw bindata
        a.resetRawData( bindata, sizeof(bindata) ); // finished

Example II (you don't want to do this):

        static char bindata[] = { 231, 1, 44, ... };
        QByteArray  a, b;
        a.setRawData( bindata, sizeof(bindata) );   // a points to bindata
        a.resize( 8 );                              // will crash
        b = a;                                      // will crash
        a[2] = 123;                                 // might crash
          // forget to resetRawData - will crash

Warning: If you do not call resetRawData(), QArray will attempt to deallocate or reallocate the raw data, which might not be too good. Be careful.

See also resetRawData().

### uint QByteArray::size () const

Returns the size of the array (max number of elements).

The array is a null array if size() == 0.

See also isNull() and resize().

### void QByteArray::sort ()

Sorts the array elements in ascending order, using bitwise comparison (memcmp()).

See also bsearch().

### bool QByteArray::truncate ( uint pos )

Truncates the array at position pos.

Returns TRUE if successful, or FALSE if the memory cannot be allocated.

Equivalent to resize(pos).

See also resize().

## Related Functions

### Q_UINT16 qChecksum (const char * data, uint len)

Returns the CRC-16 checksum of len bytes starting at data.

The checksum is independent of the byte order (endianness).

### QDataStream & operator<< (QDataStream & s, const QByteArray & a)

Writes a byte array to a stream and returns a reference to the stream.

See also Format of the QDataStream operators

### QDataStream & operator>> (QDataStream & s, QByteArray & a)

Reads a byte array from a stream and returns a reference to the stream.

See also Format of the QDataStream operators

Search the documentation, FAQ, qt-interest archive and more (uses www.trolltech.com):

This file is part of the Qt toolkit, copyright © 1995-2000 Trolltech, all rights reserved.