# PIP: PIByteArray Class Reference

PIByteArray Class Reference

Byte array. More...

Inherits PIDeque< T >.

## Classes

struct | RawData
| Help struct to store/restore custom blocks of data to/from PIByteArray. More...

## Public Member Functions

| PIByteArray ()
| Constructs an empty byte array.
| PIByteArray (const uint size)
| Constructs 0-filled byte array with size "size".
| PIByteArray (const void *data, const uint size)
| Constructs byte array from data "data" and size "size".
PIByteArray | resized (int new_size) const
| Return resized byte array.
PIByteArray & | convertToBase64 ()
| Convert data to Base 64 and return this byte array.
PIByteArray & | convertFromBase64 ()
| Convert data from Base 64 and return this byte array.
PIByteArray | toBase64 () const
| Return converted to Base 64 data.
PIByteArray | fromBase64 () const
| Return converted from Base 64 data.
PIByteArray & | append (const void *data_, int size_)
| Add to the end data "data" with size "size".
PIByteArray & | append (const PIByteArray &data_)
| Add to the end byte array "data".
uchar | checksumPlain8 () const
| Returns plain 8-bit checksum.
uint | checksumPlain32 () const
| Returns plain 32-bit checksum.

## Related Functions

(Note that these are not member functions.)
std::ostream & | operator<< (std::ostream &s, const PIByteArray &ba)
| Output to std::ostream operator.
PICout | operator<< (PICout s, const PIByteArray &ba)
| Output to PICout operator.
PIByteArray & | operator<< (PIByteArray &s, uchar v)
| Store operator.
PIByteArray & | operator<< (PIByteArray &s, const short v)
| Store operator.
PIByteArray & | operator<< (PIByteArray &s, const int v)
| Store operator.
PIByteArray & | operator<< (PIByteArray &s, const long &v)
| Store operator.
PIByteArray & | operator<< (PIByteArray &s, const llong &v)
| Store operator.
PIByteArray & | operator<< (PIByteArray &s, const ushort v)
| Store operator.
PIByteArray & | operator<< (PIByteArray &s, const uint v)
| Store operator.
PIByteArray & | operator<< (PIByteArray &s, const ulong &v)
| Store operator.
PIByteArray & | operator<< (PIByteArray &s, const ullong &v)
| Store operator.
PIByteArray & | operator<< (PIByteArray &s, const float v)
| Store operator.
PIByteArray & | operator<< (PIByteArray &s, const double &v)
| Store operator.
PIByteArray & | operator<< (PIByteArray &s, const PIByteArray &v)
| Store operator, see Attention for details.
PIByteArray & | operator<< (PIByteArray &s, const PIByteArray::RawData &v)
| Store operator, see Attention for details.
template<typename Type0 , typename Type1 >
PIByteArray & | operator<< (PIByteArray &s, const PIPair< Type0, Type1 > &v)
| Store operator.
template<typename T >
PIByteArray & | operator<< (PIByteArray &s, const PIVector< T > &v)
| Store operator.
template<typename T >
PIByteArray & | operator<< (PIByteArray &s, const PIList< T > &v)
| Store operator.
template<typename T >
PIByteArray & | operator<< (PIByteArray &s, const PIDeque< T > &v)
| Store operator.
PIByteArray & | operator>> (PIByteArray &s, uchar &v)
| Restore operator.
PIByteArray & | operator>> (PIByteArray &s, short &v)
| Restore operator.
PIByteArray & | operator>> (PIByteArray &s, int &v)
| Restore operator.
PIByteArray & | operator>> (PIByteArray &s, long &v)
| Restore operator.
PIByteArray & | operator>> (PIByteArray &s, llong &v)
| Restore operator.
PIByteArray & | operator>> (PIByteArray &s, ushort &v)
| Restore operator.
PIByteArray & | operator>> (PIByteArray &s, uint &v)
| Restore operator.
PIByteArray & | operator>> (PIByteArray &s, ulong &v)
| Restore operator.
PIByteArray & | operator>> (PIByteArray &s, ullong &v)
| Restore operator.
PIByteArray & | operator>> (PIByteArray &s, float &v)
| Restore operator.
PIByteArray & | operator>> (PIByteArray &s, double &v)
| Restore operator.
PIByteArray & | operator>> (PIByteArray &s, PIByteArray &v)
| Restore operator, see Attention for details.
PIByteArray & | operator>> (PIByteArray &s, PIByteArray::RawData v)
| Restore operator, see Attention for details.
template<typename Type0 , typename Type1 >
PIByteArray & | operator>> (PIByteArray &s, PIPair< Type0, Type1 > &v)
| Restore operator.
template<typename T >
PIByteArray & | operator>> (PIByteArray &s, PIVector< T > &v)
| Restore operator.
template<typename T >
PIByteArray & | operator>> (PIByteArray &s, PIList< T > &v)
| Restore operator.
template<typename T >
PIByteArray & | operator>> (PIByteArray &s, PIDeque< T > &v)
| Restore operator.
bool | operator== (PIByteArray &f, PIByteArray &s)
| Byte arrays compare operator.
bool | operator!= (PIByteArray &f, PIByteArray &s)
| Byte arrays compare operator.
PIByteArray & | operator<< (PIByteArray &s, const PIString &v)
| Output operator to PIByteArray.
PIByteArray & | operator>> (PIByteArray &s, PIString &v)
| Input operator from PIByteArray.
PIByteArray & | operator<< (PIByteArray &s, const PIStringList &v)
| Output operator to PIByteArray.
PIByteArray & | operator>> (PIByteArray &s, PIStringList &v)
| Input operator from PIByteArray.
PICout | operator<< (PICout s, const PISystemTime &v)
| Output operator to PICout.
PIByteArray & | operator<< (PIByteArray &s, const PISystemTime &v)
| Output operator to PIByteArray.
PIByteArray & | operator>> (PIByteArray &s, PISystemTime &v)
| Input operator from PIByteArray.

## Detailed Description

Byte array.

This class based on PIDeque<uchar> and provide some handle function to manipulate it.

# Usage

PIByteArray can be used to store custom data and manipulate it. There are many stream operators to store/restore common types to byte array. Store operators places data at the end of array, restore operators takes data from the beginning of array. In addition there are Base 64 convertions and checksums:

- plain 8-bit
- plain 32-bit

One of the major usage of PIByteArray is stream functions. You can form binary packet from many types (also dynamic types, e.g. PIVector) with one line:

PIByteArray ba;
int i = -1, j = 2;
float f = 1.;
PIString text("123");
ba << i << j << f << text; // form binary data
piCout << "data =" << ba;
i = j = 0; // clear variables
f = 0; // clear variables
text.clear(); // clear variables
piCout << i << j << f << text; // show variables
ba >> i >> j >> f >> text; // restore data
piCout << i << j << f << text; // show variables
piCout << "data =" << ba;

Or you can descibe stream operator of your own type and store/restore vectors of your type:

struct MyType {
MyType(int i_ = 0, const PIString & t_ = PIString()) {
m_i = i_;
m_text = t_;
}
int m_i;
PIString m_text;
};
inline PIByteArray & operator <<(PIByteArray & s, const MyType & v) {s << v.m_i << v.m_text; return s;}
inline PIByteArray & operator >>(PIByteArray & s, MyType & v) {s >> v.m_i >> v.m_text; return s;}
PIByteArray ba;
PIVector<MyType> my_vec;
my_vec << MyType(1, "s1") << MyType(10, "s10"); // add to vector
ba << my_vec; // store to byte array
piCout << "data =" << ba;
my_vec.clear(); // clear vector
ba >> my_vec; // restore from byte array

For store/restore custom data blocks there is PIByteArray::RawData class. Stream operators of this class simply store/restore data block to/from byte array.

PIByteArray ba;
const char * chars = "8 bytes";
ba << PIByteArray::RawData(chars, 8); // form binary data
piCout << "data =" << ba;
char rchars[16];
memset(rchars, 0, 16); // clear data
ba >> PIByteArray::RawData(rchars, 8); // restore data
piCout << rchars;
piCout << "data =" << ba;

# Attention

Stream operator of PIByteArray store byte array as vector, not simply append content of byte array. This operators useful to transmit custom data as PIByteArray packed into parent byte array, e.g. to form packet from PIByteArray. To append one byte array to another use funtion append().

PIByteArray ba, sba;
uchar uc(127);
sba << uc; // byte array with one byte
ba << sba; // stream operator
piCout << ba; // result
// {1, 0, 0, 0, 127}
ba.clear();
ba.append(sba);
piCout << ba; // result
// {127}