CXI
Loading...
Searching...
No Matches
Public Member Functions | Friends | List of all members
ByteArray Class Reference

Encapsulates an array of primitive type char in an object and provides methods to operate on binary data. More...

Inheritance diagram for ByteArray:
[legend]

Public Member Functions

 ByteArray (void)
 
 ByteArray (int len)
 
 ByteArray (const char *data, int len)
 
 ByteArray (const char *str)
 
 ByteArray (int val, int len)
 
 ByteArray (const ByteArray &ba)
 
virtual ~ByteArray (void)
 
void clear (void)
 
int length (void) const
 
char * get (void)
 
const char * get (void) const
 
void set (const char *data, int len)
 
void append (const char *data, int len)
 
void append (const ByteArray &ba)
 
void insert (const char *data, int len, int offset)
 
void fill (char value, int len, int offset)
 
int getInt (int def_val=0) const
 
void setInt (int val, int len)
 
void appendInt (int val, int len)
 
void getString (char *str, int max_size) const
 
void setString (const char *str)
 
void appendString (const char *str)
 
std::string toString (void) const
 
std::string toHexString (void) const
 
int compare (const ByteArray &ba) const
 
ByteArray sub (int offset, int len=-1) const
 
ByteArray lstrip (void) const
 
ByteArray rstrip (const std::set< char > &charsToBeStripped) const
 
void read (const char *filename)
 
void write (const char *filename) const
 
void xtrace (const char *text=0) const
 
ByteArrayoperator= (const ByteArray &ba)
 
const ByteArrayoperator+= (const ByteArray &ba)
 
const ByteArrayoperator|= (const ByteArray &ba)
 
const ByteArrayoperator^= (const ByteArray &ba)
 
char & operator[] (int idx)
 

Friends

CXIAPI friend ByteArray operator+ (const ByteArray &, const ByteArray &)
 
CXIAPI friend ByteArray operator+ (const ByteArray &, const ByteArray &)
 

Detailed Description

Encapsulates an array of primitive type char in an object and provides methods to operate on binary data.

The following example illustrates the usage of the ByteArray class:

// construct a byte array
ByteArray a = ByteArray("1234", 4);
// construct a byte array from a string
ByteArray b = ByteArray("Any Data: ");
// concatenate arrays
ByteArray c = b + a;
// dump array
c.xtrace("c");
// write byte array to a file
c.write("d:/temp/byte.array");
Encapsulates an array of primitive type char in an object and provides methods to operate on binary d...
Definition: sw/cxi_api_c/def/bytearray.h:22
void xtrace(const char *text=0) const
Definition: bytearray.cpp:611
ByteArray(void)
Definition: bytearray.cpp:89
void write(const char *filename) const
Definition: bytearray.cpp:593

Constructor & Destructor Documentation

◆ ByteArray() [1/6]

ByteArray ( void  )

Default Constructor: creates empty byte array.

◆ ByteArray() [2/6]

ByteArray ( int  len)

Overload Contructor: creates byte array with given size.

Parameters
lensize of new byte array

◆ ByteArray() [3/6]

ByteArray ( const char *  data,
int  len 
)

Overload Contructor: creates byte array from given data.

Parameters
datapointer to data to be set
lenlength of data to be set

◆ ByteArray() [4/6]

ByteArray ( const char *  str)

Overload Contructor: creates byte array from given C-string.

Parameters
strC-string to be set

◆ ByteArray() [5/6]

ByteArray ( int  val,
int  len 
)

Overload Contructor: creates byte array from given integer value.

Parameters
valinteger value to be converted
lennumber of bytes to be converted [1..4]

◆ ByteArray() [6/6]

ByteArray ( const ByteArray ba)

Copy Constructor: creates byte array from given byte array.

◆ ~ByteArray()

~ByteArray ( void  )
virtual

Destructor: frees the internal data buffer.

Member Function Documentation

◆ clear()

void clear ( void  )

Frees internal data buffer

◆ length()

int length ( void  ) const

Returns length of data.

Returns
size of internal data buffer

◆ get() [1/2]

char * get ( void  )

Returns data.

Returns
pointer to internal data buffer

◆ get() [2/2]

const char * get ( void  ) const

Returns data.

Returns
pointer to internal data buffer

◆ set()

void set ( const char *  data,
int  len 
)

Creates byte array from given parameters.

Parameters
datapointer to data to be set
lenlength of data to be set
Exceptions
cxi::Exceptionif memory allocation fails

◆ append() [1/2]

void append ( const char *  data,
int  len 
)

Appends given data to byte array.

Parameters
datapointer to data to be appended
lenlength o data to be appended
Exceptions
cxi::Exceptionif memory reallocation failes

◆ append() [2/2]

void append ( const ByteArray ba)

Appends given byte array

Parameters
baByteArray to be appended
Exceptions
cxi::Exceptionif memory allocation failes

◆ insert()

void insert ( const char *  data,
int  len,
int  offset 
)

Inserts given data at given poistion

Parameters
datadata to be inserted
lendata length
offsetinsertation position
Exceptions
cxi::Exceptionif offset+len > size

◆ fill()

void fill ( char  value,
int  len,
int  offset 
)

Fills portion of byte array with given pattern.

Parameters
valuepattern to be set
lenlength of area the be filled
offsetstart of ares to be filled
Exceptions
cxi::Exceptionif offset+len > size

◆ getInt()

int getInt ( int  def_val = 0) const

Returns big-endian representation of byte array as integer.
The conversion regards up to 4 bytes, succeeding bytes in the data buffer are omitted.

Parameters
def_valdefault value to be return if the byte array doesn't contain data (size == 0)

◆ setInt()

void setInt ( int  val,
int  len 
)

Loads byte array with big-endian representation of the given integer.

Parameters
valinteger value to be converted
lennumber of bytes to be converted [1..4]
Exceptions
cxi::Exceptionif memory allocation fails

◆ appendInt()

void appendInt ( int  val,
int  len 
)

Appends big-endian representation of the given integer to byte array.

Parameters
valinteger value to be converted
lennumber of bytes to be converted [1..4]
Exceptions
cxi::Exceptionif memory reallocation fails

◆ getString()

void getString ( char *  str,
int  max_size 
) const

Returns byte array data as zero-terminated C-string.

Parameters
strpointer to buffer where string is stored
max_sizesize of buffer

◆ setString()

void setString ( const char *  str)

Creates byte array from given C-string.

Parameters
strzero-terminated C-string to be loaded.

◆ appendString()

void appendString ( const char *  str)

Appends given C-string to byte array.

Parameters
strzero-terminated C-string to be appended
Exceptions
cxi::Exceptionif memory reallocation fails

◆ toString()

std::string toString ( void  ) const

Returns string representation of byte array without trailing zeros.

◆ toHexString()

std::string toHexString ( void  ) const

Returns hexadecimal representation of byte array.

◆ compare()

int compare ( const ByteArray ba) const

Compares byte array with another

Parameters
babyte array to be compared
Returns
  • zero if content of both arrays is the same
  • positive if given byte array is greater
  • negative if given byte array is smaller

◆ sub()

ByteArray sub ( int  offset,
int  len = -1 
) const

Returns portion of the byte array

Parameters
offsetzero base offset
lennumber of bytes to be copied
Returns
byte array containing desired portion
Exceptions
cxi::Exceptionif offset+len > size

◆ lstrip()

ByteArray lstrip ( void  ) const

Strips leading zeros

Returns
stripped byte array

◆ rstrip()

ByteArray rstrip ( const std::set< char > &  charsToBeStripped) const

Strips character from rear

Returns
stripped byte array

◆ read()

void read ( const char *  filename)

Reads byte array from file

Parameters
filenamename of file to be read
Exceptions
cxi::Exceptionif file doesn't exist or memory allocation failed

◆ write()

void write ( const char *  filename) const

Writes byte array to file

Parameters
filenamename of file to be written
Exceptions
cxi::Exceptionif file can't be opened

◆ xtrace()

void xtrace ( const char *  text = 0) const

Prints content of byte array to stdout. The binary data is converted to hexadecimal notation.

Parameters
texttag (optional)

◆ operator=()

ByteArray & operator= ( const ByteArray ba)

Copies one instance of byte array to another.

Parameters
basource byte array
Exceptions
cxi::Exceptionif memory allocation fails

◆ operator+=()

const ByteArray & operator+= ( const ByteArray ba)

Appends given byte array.

Parameters
babyte array to be appended
Exceptions
cxi::Exceptionif memory reallocation fails

◆ operator|=()

const ByteArray & operator|= ( const ByteArray ba)

ORs the given byte array

Parameters
babyte array to be ORed Note:
  • size of both byte array have to be equal
Exceptions
cxi::Exceptionif byte arrays don't have the same length

◆ operator^=()

const ByteArray & operator^= ( const ByteArray ba)

XORs the given byte array

Parameters
babyte array to be XORed Note:
  • size of both byte array have to be equal
Exceptions
cxi::Exceptionif byte arrays don't have the same length

◆ operator[]()

char & operator[] ( int  idx)

Get one byte from array.

Parameters
idxzero based byte index
Returns
desired byte

Friends And Related Function Documentation

◆ operator+ [1/2]

CXIAPI friend ByteArray operator+ ( const ByteArray ba1,
const ByteArray ba2 
)
friend

Concatenates two byte arrays.

Parameters
ba1first byte array
ba2second byte array to be concatenated
Returns
concatenated byte array
Exceptions
cxi::Exceptionif memory allocation fails

◆ operator+ [2/2]

CXIAPI friend ByteArray operator+ ( const ByteArray ba1,
const ByteArray ba2 
)
friend

Concatenates two byte arrays.

Parameters
ba1first byte array
ba2second byte array to be concatenated
Returns
concatenated byte array
Exceptions
cxi::Exceptionif memory allocation fails